title: Winsock2 listen loop
body: <p>Hello again wonderful stackoverflow community! Last time I asked a question, it was resolved quickly, right off the back and I hope this will go the same way. :)</p>
  
  <p>So I'm toying with winsock, and I want to be able to connect multiple clients to my server simultaneously. I feel that this could be achieved with a loop on thread creation, socket creation, binding, and listening every time a client connects, but my efforts to do so have only turned up with "listen failed with error"'s. Two different ones depending on what I had tried.
  I googled it only to find the advanced MSDN samples the easiest, and still way too hard, examples out there.
  Anyone have any simple suggestions?
  (Side question: I can't seem to get "mrecv()" to return the whole "recvbuf" variable. All I get is one letter. I <em>know</em> this is a newbie mistake I'm making, but I just can't figure it out. :/ This problem can wait until later, however.)</p>
  
  <p>(Here's the server code so far:)</p>
  
  <pre><code>#undef UNICODE
  
  #define WIN32_LEAN_AND_MEAN
  
  #include &lt;windows.h&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  
  // Need to link with Ws2_32.lib
  #pragma comment (lib, "Ws2_32.lib")
  // #pragma comment (lib, "Mswsock.lib")
  
  int minitialize();
  int msend(char msendbuf[512]);
  char mrecv();
  int mshutdown();
  
  #define DEFAULT_BUFLEN 512
  #define DEFAULT_PORT "10150"
   WSADATA wsaData;
   int iResult;
  
   SOCKET ListenSocket = INVALID_SOCKET;
   SOCKET ClientSocket = INVALID_SOCKET;
  
   struct addrinfo *result = NULL;
   struct addrinfo hints;
  
   int iSendResult;
   char recvbuf[DEFAULT_BUFLEN];
   int recvbuflen = DEFAULT_BUFLEN;
  
  int main(void) 
  {
      minitialize();
      mrecv();
  
      char mmessage[512];
      if (strncmp(mmessage,"shutdown",(strlen(mmessage))) == 0) {mshutdown();}
      std::cin.getline(mmessage, 512);
      msend(mmessage);
  
      // shutdown the connection since we're done
      mshutdown();
      std::cin.ignore();
  
      return 0;
  }
  
  int msend(char msendbuf[512])   // Send a message
  {
      int iResult3 = send( ClientSocket, msendbuf, 512, 0 );
      if (iResult3 == SOCKET_ERROR) {
          printf("send failed with error: %d\n", WSAGetLastError());
          closesocket(ClientSocket);
          WSACleanup();
          return 1;
      }
  
      printf("Bytes Sent: %ld\n", iResult);
              std::cout&lt;&lt;"msendbuf: "&lt;&lt;msendbuf&lt;&lt;"\n";
              std::cin.ignore();
  }
  
  char mrecv() //Recieve a message
  {
      int iResult2 = recv(ClientSocket, recvbuf, 512, 0);
          if (iResult2 &gt; 0) {
              printf("Bytes received: %d\n", iResult2);
          std::cout&lt;&lt;"recvbuf: "&lt;&lt;recvbuf&lt;&lt;"\n";
          }
          else if (iResult2 == 0)
              printf("Connection closing...\n");
          else  {
              printf("recv failed with error: %d\n", WSAGetLastError());
              closesocket(ClientSocket);
              WSACleanup();
              return 1;
          }
      return *recvbuf;
  }
  
  int minitialize()   //initialize the winsock server
  {
      // Initialize Winsock
      iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      if (iResult != 0) {
          printf("WSAStartup failed with error: %d\n", iResult);
          return 1;
      }
  
      ZeroMemory(&amp;hints, sizeof(hints));
      hints.ai_family = AF_INET;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_protocol = IPPROTO_TCP;
      hints.ai_flags = AI_PASSIVE;
  
      // Resolve the server address and port
      iResult = getaddrinfo(NULL, DEFAULT_PORT, &amp;hints, &amp;result);
      if ( iResult != 0 ) {
          printf("getaddrinfo failed with error: %d\n", iResult);
          WSACleanup();
          return 1;
      }
  
      // Create a SOCKET for connecting to server
      ListenSocket = socket(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);
      if (ListenSocket == INVALID_SOCKET) {
          printf("socket failed with error: %ld\n", WSAGetLastError());
          freeaddrinfo(result);
          WSACleanup();
          return 1;
      }
  
      // Setup the TCP listening socket
      iResult = bind( ListenSocket, result-&gt;ai_addr, (int)result-&gt;ai_addrlen);
      if (iResult == SOCKET_ERROR) {
          printf("bind failed with error: %d\n", WSAGetLastError());
          freeaddrinfo(result);
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      freeaddrinfo(result);
  
      iResult = listen(ListenSocket, SOMAXCONN);
      if (iResult == SOCKET_ERROR) {
          printf("listen failed with error: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      // Accept a client socket
      ClientSocket = accept(ListenSocket, NULL, NULL);
      if (ClientSocket == INVALID_SOCKET) {
          printf("accept failed with error: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      // No longer need server socket
      closesocket(ListenSocket);
  }
  
  int mshutdown()     //shutdown the server
  {
      iResult = shutdown(ClientSocket, SD_SEND);
      if (iResult == SOCKET_ERROR) {
          printf("shutdown failed with error: %d\n", WSAGetLastError());
          closesocket(ClientSocket);
          WSACleanup();
          return 1;
      }
  
      // cleanup
      closesocket(ClientSocket);
      WSACleanup();
      return 0;
  }
  </code></pre>
  
  <p>Yes, if you're wondering, I am planning on putting all those global variables in their respective local functions. I just need them there to clear a little clutter for now, and it doesn't seem to be causing any issues.</p>