title: TCP/IP server in C++ is not waiting for client
body: <p>I found some code in the internet with some basics about creating an TCP/IP server in C++.
  I took this code, added some libraries and some lines of code. The result is, that I am able to compile and run the code/server in Qt Creator on Windows as a console program. Nevertheless I am not able to connect to the server because it just runs through the code without waiting and accepting a connection from an TCP client.</p>
  
  <p>This is the code:
  EDIT: Code is updated and it's now working. Test against == -1 is the solution.</p>
  
  <pre><code>#define PORT 2000
  #define MAX_QUEUE 5
  #define BUF_SIZE 1024
  
  int main(int argc, char* const argv[])
  {
  cout &lt;&lt; "Hello World!" &lt;&lt; endl;    
          WSADATA wsaData;
  int wsaret = WSAStartup(MAKEWORD( 4, 4 ), &amp;wsaData);
  cout &lt;&lt; "wsaret: " &lt;&lt; wsaret &lt;&lt; endl;
  
  cout &lt;&lt; "Start TCP/IP Server" &lt;&lt; endl;
  
  /* file descriptors for sockets */
  SOCKET sock_1, sock_2;  //switched from in to SOCKET //unsigned int sock_1, sock_2;
  int rec_value, length;
  char buf[BUF_SIZE];
  
  //struct sockaddr_in server;
  struct sockaddr_in server;
  
  /* create stream socket in internet domain*/
  sock_1 = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  //if(sock_1 &lt; 0)
  if(sock_1 == -1)
  {
      cout &lt;&lt; "socket: " &lt;&lt; WSAGetLastError() &lt;&lt; endl; //instead of //cout &lt;&lt; "socket: " &lt;&lt; strerror(errno) &lt;&lt; endl; //cout &lt;&lt; "socket wsaret: " &lt;&lt; wsaret &lt;&lt; endl;
      exit(1);
  }
  
  /* build address in internet domain */
  server.sin_family = AF_INET;
  
  /* everyone is allowed to connet to server */
  server.sin_addr.s_addr = INADDR_ANY; //inet_addr("192.168.145.129");
  server.sin_port = htons(2000);  //server.sin_port = PORT; //Port: 2000
  
  /* bind socket */
  //if(bind(sock_1, (struct sockaddr *)&amp;server, sizeof(server)));
  int bindreturn = (bind(sock_1, (struct sockaddr *)&amp;server, sizeof(server)));
  if(bindreturn == -1)
  {
      cout &lt;&lt; "bind: " &lt;&lt; WSAGetLastError() &lt;&lt; endl; //instead of //cout &lt;&lt; "bind: " &lt;&lt; strerror(errno) &lt;&lt; endl; //cout &lt;&lt; "bind wsaret: " &lt;&lt; wsaret &lt;&lt; endl;
      exit(1);
  }
  
  listen(sock_1,MAX_QUEUE);
      /* start accepting connection */
      sock_2 = accept(sock_1,0,0);
      if(sock_2 &lt; 0)
      {
          cout &lt;&lt; "accept: " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
          exit(1);
      }
  
      /* read from sock_2 */
      while(rec_value=recv(sock_2,buf,BUF_SIZE,0))
      {
          if(rec_value &lt; 0)
  
          {
           cout &lt;&lt; "recv: " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
           exit(1);
          }
          else
          {
              cout &lt;&lt; "else" &lt;&lt; endl;
              send(sock_2,"1,2,3",strlen("1,2,3"),0);
          }
      }
  
      cout &lt;&lt; "Ending connection" &lt;&lt; endl;
      closesocket(sock_1);
      closesocket(sock_2);
      cout &lt;&lt; "End TCP/IP Server" &lt;&lt; endl;
  
      WSACleanup();
  return 0;
  }
  </code></pre>
  
  <p>The console shows the following after/during running the program in one or less seconds:</p>
  
  <pre><code>Hello World!
  wsaret: 0
  Start TCP/IP Server
  bind: 0
  </code></pre>
  
  <p>What can I do, that the server waits for an connection partner?</p>
  
  <p>Thanks in advance.</p>
  
  <p>Regards
  matl</p>
  
  <p>Edit: Added a few lines of code at the beginning, which I forgot before.
  Edit: Code update
  Edit: Code update</p>