title: Checking sockets using select() - Winsock
body: <p>I have been exploring on function <code>select()</code> to check if some sockets are ready to read and I must admit that I'm a bit confused.  The MSDN says "The select function returns the total number of socket handles that are ready and contained in the <code>fd_set</code> structures". </p>
  
  <p>Suppose I have 3 sockets and 2 sockets are ready, <code>select()</code> returns 2, but this gives me no information which 2 of these 3 sockets are ready to read so how can I check it? </p>
  
  <p>On stack overflow I came across this: <a href="https://stackoverflow.com/questions/7637765/why-fd-set-fd-zero-for-select-inside-of-loop">When select returns, it has updated the sets to show which file descriptors have become ready for read/write/exception</a></p>
  
  <p>So I put breakpoints in my program to track my <code>fd_set</code> structure. What I have realized is that ( just one socket in <code>fd_set</code>): If socket is ready to read <code>select()</code>:</p>
  
  <ol>
  <li>returns 1</li>
  <li>leaves <code>fd_count</code> (The number of sockets in the set) untouched</li>
  <li>leaves <code>fd_array</code> (An array of sockets that are in the set.) untouched</li>
  </ol>
  
  <p>If client did not send any data addressed to that socket <code>select()</code>:</p>
  
  <ol>
  <li>returns 0</li>
  <li>decreases <code>fd_count</code> to 0</li>
  <li>leaves <code>fd_array</code>  untouched</li>
  </ol>
  
  <p>If I call select() again and client again sent no data:</p>
  
  <ol>
  <li>return -1 (I think this is because of the <code>fd_count</code> value - 0)</li>
  </ol>
  
  <p>I guess I miss some crucial rules how select() works and what this function does but I can't figure out it.
  Here is some code snippet to show what I do to call select():</p>
  
  <pre class="lang-cpp prettyprint-override"><code>CServer::CServer(char *ipAddress,short int portNumber)
  {   // Creating socket
      ServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
      if (ServerSocket == INVALID_SOCKET) 
          std::cout &lt;&lt; "I was not able to create  ServerSocket\n";
      else 
          std::cout &lt;&lt; "ServerSocket created successfully\n";
  
      // Initialization of ServerSocket Address
      ServerSocketAddress.sin_family = AF_INET;
      ServerSocketAddress.sin_addr.S_un.S_addr = inet_addr(ipAddress);
      ServerSocketAddress.sin_port = htons(portNumber);
      // Binding ServerSocket to ServerSocket Address
      if (bind(ServerSocket, (SOCKADDR*)&amp;ServerSocketAddress, sizeof(ServerSocketAddress)) == 0) 
          std::cout &lt;&lt; "Binding ServersSocket and ServerSocketAddress ended with success\n";
      else 
          std::cout &lt;&lt; "There were problems with binding ServerSocket and ServerSocket Address\n";
  
      // Initialization of the set of sockets
      ServerSet.fd_count = 1;
      ServerSet.fd_array[0] = ServerSocket;
  }
  </code></pre>
  
  <p>In main :</p>
  
  <pre class="lang-cpp prettyprint-override"><code>CServer Server(IP_LOOPBACK_ADDRESS, 500);
  tmp = select(0, &amp;Server.ServerSet, NULL, NULL, &amp;TimeOut);
  </code></pre>
  
  <p>Should't the <code>fd_array</code> be filled with 0 values after the <code>select()</code> call, when there is no socket that can be read?</p>