How to initialize a non blocking socket using only winsock library

This is a discussion on How to initialize a non blocking socket using only winsock library within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, im sorry for asking what must be a very simple question, but i just cant figure out (ive googled ...

  1. #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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #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.

  4. #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

  5. #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)

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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