C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 12-18-2007, 05:52 AM   #1
Registered User
 
Join Date: Jul 2006
Posts: 63
How to initialize a non blocking socket using only winsock library

Hi, im sorry for asking what must be a very simple question, but i just cant figure out (ive googled and sifted thru msdn) how to set a socket as non blocking. Is it a function which sets it as non blocking or blocking, or is it in a data structure which is used by a function? Im sorry but i just cant figure it out, so if anyone could help me it would be greatly appreciated.

Also, just out of curiosity, is a socket set as blocking/non blocking when the socket is initialized (ie socket is called), or can it be changed, say, in between two recv() calls.

Thx again
*DEAD* is offline   Reply With Quote
Old 12-18-2007, 06:02 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
As far as I understand [given that I haven't ever done socket programming], you use select to check if a socket will block. You can then safely call recv() to fetch some data on a particular socking, knowing that the call will NOT block.

Is that what you wanted to know?

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 12-18-2007, 06:17 AM   #3
Registered User
 
Join Date: Jul 2006
Posts: 63
Well, i think your talking about what happens with non-blocking sockets once they have been initialized. If you call recv and the call wouldblock, then it returns a blocking error and you must parse that and move on with the program.
*DEAD* is offline   Reply With Quote
Old 12-24-2007, 06:33 AM   #4
Registered User
 
Join Date: Dec 2007
Posts: 6
When you create a socket it is automatically set to be blocking. If you want to set it to non blocking then you can call fcntl, i.e.

Code:
fcntl(sockfd, F_SETFL, O_NONBLOCK);
however this is something i dont recommend doing as it requires you to continually check the socket and can kill the CPU.

I highly recommend you use the select method which can monitor a set of sockets and when there is an event on one of them you can then determine which one it is and work with it. You can also add a timeout interval on select so if no event is received on the sockets during that time interval then it carries on with program execution.

Let us know if you need any help using select. It is relatively easy.

Hope this helps
Codeslinger is offline   Reply With Quote
Old 01-18-2008, 07:03 AM   #5
Unregistered User
 
Join Date: Sep 2005
Location: Antarctica
Posts: 341
maybe what you are looking for is the select() function? Here's a link that may help:
http://tangentsoft.net/wskfaq/exampl...ct-server.html
__________________
-- Rocky
-- DreamSys Software (http://www.dreamsyssoft.com)
-- Free Tiff 2 PDF Library (http://www.dreamsyssoft.com/tiff-to-pdf-api)
rockytriton is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nonblocking socket...blocking? zolom Networking/Device Communication 2 07-13-2009 12:06 PM
very weird .h problem royuco77 C++ Programming 1 09-11-2005 07:55 AM
Where do I initialize Winsock and catch messages for it? Lithorien Windows Programming 10 12-30-2004 12:11 PM
problem closing socket Wisefool Networking/Device Communication 2 10-29-2003 12:19 PM
C++ Socket Library iron C++ Programming 1 06-28-2002 09:01 AM


All times are GMT -6. The time now is 06:07 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22