none
Socket : Select function returns always 0

    Question

  • Hello,

    I am using Winsock in my application. I am opening and connecting sockets but when sometimes I am blocking in the function Select, which returns always 0. 

    Does anyone know this issue?

    I meet this problem not systematically, and it seems it appears when I leave very quickly my application, as if something is not closed correctly. So I am wondering if there is a solution, to reinitialize all the connection when I restart my application and avoid this timeout.

    I hope someone can I help me, any help will be appreciated.

    Thanks a lot


    Tuesday, June 30, 2009 10:17 AM

Answers

  • Hi kikekoikan,

              This error is that address already in use.

    Typically, only one usage of each socket address (protocol/IP address/port) is permitted. This error occurs if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, or a socket that was not closed properly, or one that is still in the process of closing.

     For server applications that need to bind multiple sockets to the same port number, consider using setsockopt (SO_REUSEADDR). Client applications usually need not call bind at all—connect chooses an unused port automatically.

     When bind is called with a wildcard address (involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the specific address is committed. This could happen with a call to another function later, including connect, listen, WSAConnect, or WSAJoinLeaf.



    Best regards,
    Guang-Ming Bian - MSFT



    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Thursday, July 02, 2009 9:29 AM

All replies

  • Here is the code I am using to close my sockets :

    shutdown(pSocketContext->theSocket, SD_BOTH);
    closesocket(pSocketContext->theSocket);
    pSocketContext->theSocket = 0;
    WSACleanup();

    This code is always call each time I quit my application. But I quit it during connecting step, then the reconnection cannot be done and the function Select returns always 0.

    I am using the option SO_LINGER so the quit sequence is gracefully accroding to msdn doc. Have you an ideza about this problem please?
    Wednesday, July 01, 2009 1:51 PM
  • when I try to reconnect and it fails I get this error : WSAEADDRINUSE returned by the function connect().

    Then the function select() returns always 0, until a timeout expires.


    Wednesday, July 01, 2009 4:14 PM
  • Hi kikekoikan,

              This error is that address already in use.

    Typically, only one usage of each socket address (protocol/IP address/port) is permitted. This error occurs if an application attempts to bind a socket to an IP address/port that has already been used for an existing socket, or a socket that was not closed properly, or one that is still in the process of closing.

     For server applications that need to bind multiple sockets to the same port number, consider using setsockopt (SO_REUSEADDR). Client applications usually need not call bind at all—connect chooses an unused port automatically.

     When bind is called with a wildcard address (involving ADDR_ANY), a WSAEADDRINUSE error could be delayed until the specific address is committed. This could happen with a call to another function later, including connect, listen, WSAConnect, or WSAJoinLeaf.



    Best regards,
    Guang-Ming Bian - MSFT



    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Thursday, July 02, 2009 9:29 AM
  • Thank you for your answer Guang-Ming Bian!

    Effectively the option SO_REUSEADDR avoids the function connect()to return  WSAEADDRINUSE. And if I understand well you recommend me to not use Bind() function. 
    Thursday, July 02, 2009 2:30 PM
  • In fact, I think the problem is that the socket is badly closed. But I can't see anything I could do more than this code :

    shutdown(pSocketContext->theSocket, SD_BOTH);
    closesocket(pSocketContext->theSocket);
    pSocketContext->theSocket = 0;
    WSACleanup();

    I strongly think the socket is badly closed, because the connection failure appears after exiting in connecting state. If I leave the application in Connected State the reconnection is OK. Do you see something to do, to be sure to close all socket correctly?

    I have used the option SO_LINGER, or I have wiated a while before reconnecting but there is no improvement.

    Regards
    Thursday, July 02, 2009 2:44 PM
  • Hi kikekoilkan,

    Thank you for your reply.

    As I suggested in my previous reply:
    For server applications that need to bind multiple sockets to the same port number, consider using setsockopt (SO_REUSEADDR). Client applications usually need not call bind at all—connect chooses an unused port automatically.

    Since your application is client, the solution is binding an unsuse port and reconnect it.



    Best regards,
    Guang-Ming Bian - MSFT
    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Monday, July 06, 2009 3:37 AM
  • Thanks a lot for your help Guang-Ming Bian it was very helpful! an dnow I have no more connection problem. The connect() function chooses an unused port automatically is the information I needed.

    Have a nice day!
    Tuesday, July 07, 2009 1:03 PM
  • Hi


    I have this same problem but with an ftpd and passive ports.
    My ftpd needs to bind to port 20. Then afterwards it connects to the data port of the other side. (This is accoring to the protocol)

    After a while binding to the port 20 is still succesfull (I use SO_REUSEADDR for this)
    But the connects are failing !! (connection timed out) If I try to connect to the other port via telnet it works without problems. So the oftens binds to port 20 is certainly the problem
    Using a random port is not an option since I want to follow the protocol and bind to port 20 for the connection to the data port on the other side.

    I tried everything I could think of but no success. Any solution for this problem?


    Kind regards
    Jo
    • Edited by _JGO_ Friday, July 31, 2009 3:07 PM
    Friday, July 31, 2009 3:02 PM
  • btw I really wonder what the specialists could bring up as solution, this is bothering me for ages.
    On some systems it worse than on others for some reason.
    Friday, July 31, 2009 3:04 PM