Search code examples
c++selectwindows-xperrno

Using Select() and getting "no error" errors returned


I'm having some issues using Select() to determine when there is data available to read on a socket. I expect the socket I'm connecting to to have no data and thus for select to return a timeout or 0 value. Instead, I get -1 value and the message is "error: no error."

I have code very similar to this example: https://beej.us/guide/bgnet/html/multi/selectman.html

I've read through the select() documentation a couple times here: msdn.microsoft.com/en-us/library/windows/desktop/ms740141(v=vs.85).aspx

Any ideas would be appreciated. Thanks!

Specifics: C++, Win-XP, Microsoft Visual C++ 2010


Solution

  • You are adding your socket to the fd_set structure before you actually create the socket:

    FD_SET(s, &readfds);
    // (...)
    int iConnected = ConnectToHost(PortNum, IpAddy);
    //^ This actually calls: s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
    

    Therefore, you are adding an invalid socket to the fd_set. Create the socket before doing this:

    int iConnected = ConnectToHost(PortNum, IpAddy);
    //(...)
    FD_SET(s, &readfds);