title: WSA error 10048 when binding server sockets
body: <p>I started with the simple server <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms737629%28v=vs.85%29.aspx" rel="nofollow">tutorial</a> on the msdn website in order to learn how to use sockets in client and server applications.</p>
  
  <p>Once I was done following thet tutorial, I started adapting the client and server code into multithreaded proggrams in order to make a tchat client and server. Everything was going very well until I ran into WSA error 10048. I tried using different ports for each socket but it still did not solve the error.</p>
  
  <p>Here is my server code :</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;
  #include &lt;thread&gt;
  #include &lt;vector&gt;
  
  // Need to link with Ws2_32.lib
  #pragma comment (lib, "Ws2_32.lib")
  // #pragma comment (lib, "Mswsock.lib")
  
  //Global values
  //I put them as global values in order to get the server up and running.
  //I will try to pass them as params later on
  int iResult;
  struct addrinfo *result = NULL;
  struct addrinfo hints;
  int numClients = 0;
  SOCKET ClientSocket[5];
  std::thread** sendReceiveThread = new std::thread*[5];
  
  //Prototypes
  int listen(SOCKET ListenSocket);
  int accept(SOCKET ListenSocket);
  int sendReceive();
  int shutdownFunction(SOCKET ClientSocket);
  
  #define DEFAULT_BUFLEN 512
  #define DEFAULT_PORT1 "1016"
  #define DEFAULT_PORT2 "1017"
  #define DEFAULT_PORT3 "1018"
  #define DEFAULT_PORT4 "1019"
  #define DEFAULT_PORT5 "1020"
  
  int main()
  {
      std::cout &lt;&lt; 1 &lt;&lt; std::endl;
      WSADATA wsaData;
  
      SOCKET ListenSocket = INVALID_SOCKET;
  
  
      // Initialize Winsock
      std::cout &lt;&lt; 2 &lt;&lt; std::endl;
      iResult = WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
      if (iResult != 0) {
          std::cout &lt;&lt; 3 &lt;&lt; std::endl;
          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;
  
      std::thread ListenThread{ [ListenSocket](){listen(ListenSocket); } };
      ListenThread.join();
  
      return 0;
  }
  
  int listen(SOCKET ListenSocket)
  {
      int numPort = 1;
      std::vector&lt;std::thread*&gt; thread_vec;
      while (true)
      {
          if (numPort == 1)
          {
              // Resolve the server address and port
              std::cout &lt;&lt; 4 &lt;&lt; std::endl;
              iResult = getaddrinfo(NULL, DEFAULT_PORT1, &amp;hints, &amp;result);
              numPort++;
              if (iResult != 0) {
                  std::cout &lt;&lt; 5 &lt;&lt; std::endl;
                  printf("getaddrinfo failed with error: %d\n", iResult);
                  WSACleanup();
                  break;
              }
          }
  
          else if (numPort == 2)
          {
              // Resolve the server address and port
              std::cout &lt;&lt; 4 &lt;&lt; std::endl;
              iResult = getaddrinfo(NULL, DEFAULT_PORT2, &amp;hints, &amp;result);
              numPort++;
              if (iResult != 0) {
                  std::cout &lt;&lt; 5 &lt;&lt; std::endl;
                  printf("getaddrinfo failed with error: %d\n", iResult);
                  WSACleanup();
                  break;
              }
          }
  
          else if (numPort == 3)
          {
              // Resolve the server address and port
              std::cout &lt;&lt; 4 &lt;&lt; std::endl;
              iResult = getaddrinfo(NULL, DEFAULT_PORT3, &amp;hints, &amp;result);
              numPort++;
              if (iResult != 0) {
                  std::cout &lt;&lt; 5 &lt;&lt; std::endl;
                  printf("getaddrinfo failed with error: %d\n", iResult);
                  WSACleanup();
                  break;
              }
          }
  
          else if (numPort == 4)
          {
              // Resolve the server address and port
              std::cout &lt;&lt; 4 &lt;&lt; std::endl;
              iResult = getaddrinfo(NULL, DEFAULT_PORT4, &amp;hints, &amp;result);
              numPort++;
              if (iResult != 0) {
                  std::cout &lt;&lt; 5 &lt;&lt; std::endl;
                  printf("getaddrinfo failed with error: %d\n", iResult);
                  WSACleanup();
                  break;
              }
          }
  
          else if (numPort == 5)
          {
              // Resolve the server address and port
              std::cout &lt;&lt; 4 &lt;&lt; std::endl;
              iResult = getaddrinfo(NULL, DEFAULT_PORT5, &amp;hints, &amp;result);
              numPort++;
              if (iResult != 0) {
                  std::cout &lt;&lt; 5 &lt;&lt; std::endl;
                  printf("getaddrinfo failed with error: %d\n", iResult);
                  WSACleanup();
                  break;
              }
          }
  
          // Create a SOCKET for connecting to server
          std::cout &lt;&lt; 6 &lt;&lt; std::endl;
          ListenSocket = socket(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);
          if (ListenSocket == INVALID_SOCKET) {
              std::cout &lt;&lt; 7 &lt;&lt; std::endl;
              printf("socket failed with error: %ld\n", WSAGetLastError());
              freeaddrinfo(result);
              WSACleanup();
              break;
          }
  
          // Setup the TCP listening socket
          std::cout &lt;&lt; 8 &lt;&lt; std::endl;
          iResult = bind(ListenSocket, result-&gt;ai_addr, (int)result-&gt;ai_addrlen);
          if (iResult == SOCKET_ERROR) {
              std::cout &lt;&lt; 9 &lt;&lt; std::endl;
              printf("bind failed with error: %d\n", WSAGetLastError());
              freeaddrinfo(result);
              closesocket(ListenSocket);
              WSACleanup();
              break;
          }
  
          freeaddrinfo(result);
  
          std::cout &lt;&lt; 10 &lt;&lt; std::endl;
          iResult = listen(ListenSocket, SOMAXCONN);
          if (iResult == SOCKET_ERROR) {
              std::cout &lt;&lt; 11 &lt;&lt; std::endl;
              printf("listen failed with error: %d\n", WSAGetLastError());
              closesocket(ListenSocket);
              WSACleanup();
              break;
          }
  
  
          static std::thread AcceptThread{ [ListenSocket](){accept(ListenSocket); } };
          thread_vec.push_back(&amp;AcceptThread);
      }
      for (auto it : thread_vec) it-&gt;join();
      return 0;
  }
  
  int accept(SOCKET ListenSocket)
  {
      numClients++;
      const int currentNumClients = numClients;
      for (int i = 0; i &lt;= 5; i++)
      {
          ClientSocket[i] = INVALID_SOCKET;
      }
  
      // Accept a client socket
      std::cout &lt;&lt; 12 &lt;&lt; std::endl;
  
      std::cout &lt;&lt; 13 &lt;&lt; std::endl;
  
      ClientSocket[currentNumClients] = accept(ListenSocket, NULL, NULL);
      if (ClientSocket[currentNumClients] == INVALID_SOCKET)
      {
          printf("accept failed with error: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      sendReceiveThread[currentNumClients] = new std::thread([](){sendReceive(); });
      (*sendReceiveThread[currentNumClients]).join();
      delete sendReceiveThread[currentNumClients];
  
      return 0;
  }
  
  int sendReceive()
  {
      int currentNumClients = numClients;
      int iSendResult;
      char recvbuf[DEFAULT_BUFLEN];
      int recvbuflen = DEFAULT_BUFLEN;
  
      // Receive until the peer shuts down the connection
      while(true)
      {
          std::cout &lt;&lt; 14 &lt;&lt; std::endl;
          iResult = recv(ClientSocket[currentNumClients], recvbuf, recvbuflen, 0);
          std::cout &lt;&lt; iResult &lt;&lt; std::endl;
          if (iResult &gt; 0) {
              std::cout &lt;&lt; 15 &lt;&lt; std::endl;
              printf("Bytes received: %d\n", iResult);
  
              // Echo the buffer back to the clients
              std::cout &lt;&lt; 16 &lt;&lt; std::endl;
              for (int i = 1; i &lt;= numClients; i++)
              {
                  iSendResult = send(ClientSocket[currentNumClients], recvbuf, iResult, 0);
                  if (iSendResult == SOCKET_ERROR) {
                      std::cout &lt;&lt; 17 &lt;&lt; std::endl;
                      printf("send failed with error: %d\n", WSAGetLastError());
                      closesocket(ClientSocket[currentNumClients]);
                      WSACleanup();
                      return 1;
                  }
                  printf("Bytes sent: %d\n", iSendResult);
              }
          }
          else if (iResult == 0) {
              std::cout &lt;&lt; 18 &lt;&lt; std::endl;
              printf("Connection closing...\n");
              break;
          }
          else {
              std::cout &lt;&lt; 19 &lt;&lt; std::endl;
              printf("recv failed with error: %d\n", WSAGetLastError());
              std::cout &lt;&lt; "On client #" &lt;&lt; currentNumClients &lt;&lt; std::endl;
              break;
          }
  
      }
  
      iResult = shutdownFunction(ClientSocket[currentNumClients]);
  
      std::cout &lt;&lt; 22 &lt;&lt; std::endl;
      // cleanup
      closesocket(ClientSocket[currentNumClients]);
      WSACleanup();
  
      return 0;
  }
  
  int shutdownFunction(SOCKET ClientSocket)
  {
      std::cout &lt;&lt; 20 &lt;&lt; std::endl;
      iResult = shutdown(ClientSocket, SD_SEND);
      if (iResult == SOCKET_ERROR) {
          std::cout &lt;&lt; 21 &lt;&lt; std::endl;
          printf("shutdown failed with error: %d\n", WSAGetLastError());
          closesocket(ClientSocket);
          WSACleanup();
          return 1;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>You might notice the different couts, those are just couts to know how the proggram behaves.</p>