Thread: how to intilise a non blocking socket?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    how to intilise a non blocking socket?

    sorry guys for asking this stupid question but i cant find anything like how to intilise a non blocking all stuff i searched on google says some junk stuff and no code for it it would be great if someone showed a simple code with use of non blocking on it thanks .

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    You can use fcntl() to change a socket to be blocking or nonblocking.
    Code:
    result = fcntl(sck, F_GETFL, NULL);
    //This sets the socket to nonblocking
    result |= O_NONBLOCK;
    Code:
    result = fcntl(sck, F_GETFL, NULL);
    //This sets the socket to blocking
    result &= (~O_NONBLOCK);
    Might also be a good idea to check if result < 0 from the call to fcntl, which indicates an error.

    Edit: This is after you have actually created the socket (sck = socket())

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yes but i think that function is linux right ?not win?

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    So sorry, just making blind assumptions.

    I think the equivalent would be basically the same thing but using ioctlsocket() with a FIONBIO flag.

    msdn:
    Code:
    //-------------------------
    // Set the socket I/O mode: In this case FIONBIO
    // enables or disables the blocking mode for the 
    // socket based on the numerical value of iMode.
    // If iMode = 0, blocking is enabled; 
    // If iMode != 0, non-blocking mode is enabled.
    u_long iMode = 0;
    ioctlsocket(m_socket, FIONBIO, &iMode);

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Note that winsock is almost a direct clone of the BSD sockets library, which is what Linux uses . . .
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Quote Originally Posted by carrotcake1029 View Post
    So sorry, just making blind assumptions.

    I think the equivalent would be basically the same thing but using ioctlsocket() with a FIONBIO flag.

    msdn:
    Code:
    //-------------------------
    // Set the socket I/O mode: In this case FIONBIO
    // enables or disables the blocking mode for the 
    // socket based on the numerical value of iMode.
    // If iMode = 0, blocking is enabled; 
    // If iMode != 0, non-blocking mode is enabled.
    u_long iMode = 0;
    ioctlsocket(m_socket, FIONBIO, &iMode);
    then i can make connect normal ?

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    anyways let me post my problem code in other thread . thanks for the help guys.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh i forgot WSAstartup stupid me

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, 12:06 PM
  2. EAGAIN with write on a non blocking socket
    By mynickmynick in forum Networking/Device Communication
    Replies: 0
    Last Post: 04-30-2009, 09:59 AM
  3. How to initialize a non blocking socket using only winsock library
    By *DEAD* in forum Networking/Device Communication
    Replies: 4
    Last Post: 01-18-2008, 07:03 AM
  4. socket blocking problem
    By killerbyte in forum Linux Programming
    Replies: 2
    Last Post: 05-21-2006, 03:25 PM
  5. Socket Blocking Trouble!
    By LearningMan in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2003, 10:09 AM