No announcement yet.

selectsocket() on Non-Blocking Socket

  • Filter
  • Time
  • Show
Clear All
new posts

  • selectsocket() on Non-Blocking Socket

    Me again... I have a problem with selectsocket() and a non blocking socket:
    1. I create a socket set it to non-blocking,
    2. I bind() the socket,
    3. I connect() the socket, which of course fails with the error code EINPROGRESS,
    4. I then set the write flag (FD_SET(socketindex, &fds_write)) for the socket and do selectsocket().... this immediately returns so I mark the socket active... HOWEVER the socket has not connected yet!!!!
    Is this expected behaviour? or is my (somewhat limited) understanding of non-blocking sockets incorrect...
    how do I test for a connection if selectsocket() always returns true?
    I do not really want to use send() but if it's the only way....
    Any suggestions/comments welcome
    Many Thanks

  • #2
    Non-blocking sockets

    Up to this point, the only application of non-blocking sockets that I have seen have been for data transfer, not for connection establishment.

    From quick inspection of the selectsocket implementation, I does appear to me that you have the right approach. Can you post a small code snippet that fails for you? Then we can attempt to reproduce your problem.


    • #3
      Although this is an old thread, I am able to reproduce this problem with a XPort Pro device running Evolution OS V5.2.0.5B13. When using a non blocking socket to connect, the function selectsocket() can return that the socket is writeable, although the socket connection has not been established.

      Here some steps to reproduce it:
      - clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
      - ioctlsocket(clientSock, FIONBIO, &socketMode); // socketMode is 0x1 -> non blocking
      - connect(clientSock, (struct sockaddr *) &clientAddr, sizeof(clientAddr)); // errno returns EINPROGRESS/EWOULDBLOCK
      - FD_ZERO(&socketMask); FD_SET(clientSock, &socketMask); memzero(&timeout, sizeof(timeout)); selectsocket((clientSock + 1), NULL, &socketMask, NULL, &timeout); // call it several times, until return value > 0 or timeout condition

      The last function call to selectsocket() returns 0, if the XPP device does not get a response. If the XPP gets a RST/ACK (port closed), selectsocket() returns 1, which means that the socket is now writable, although it is not (a send() call returns error ECONNRESET). If XPP does not get an answer from the remote host, selectsocket() still returns 0 and that is correct.

      Other socket implementations still return 0, if a RST flag is returned by the remote host.

      Scook, is there any other possibility to do a connect() with a specified timeout value?

      Please let me know if you are able to reproduce the problem or if you need more information.