title: Error 10061 - server listening but client not connecting
body: <p>Windows 10, Microsoft Visual Studio 2019, C++</p>
  <p>I am learning TCP communication on my own, and I am using the <a href="https://docs.microsoft.com/en-us/windows/win32/api/winsock2/" rel="nofollow noreferrer">WinSock2 information page</a> to build a simple program where I can send data from myself to myself.</p>
  <p>I created the server side of the program in my <code>main.cpp</code> file, but I am unsure where to place the code of the client side.</p>
  <p>My code is a duplicate of the provided example code on Microsoft's website, as I want to get the example to run before writing my own program which will use threads. <a href="https://docs.microsoft.com/en-us/windows/win32/winsock/complete-server-code" rel="nofollow noreferrer">Server Code</a>, <a href="https://docs.microsoft.com/en-us/windows/win32/winsock/complete-client-code" rel="nofollow noreferrer">Client Code</a>.</p>
  <p>I have placed the server and client codes in separate programs, which I am running together. Both programs are on my laptop. First, I click run on the server, then a few seconds later I click run on the client. The issue is the client is not able to connect to the server (receiving error code 10061). I have tried many different port numbers, but none have worked (around ten different numbers, ranging from 1024 to 50000).</p>
  <p>The strange thing is, when I open a command prompt window and type: <code>netstat -abno | findstr &lt;port_number&gt;</code>, it tells me there is a TCP address listening. It does not show anything before or after running the program, so I am certain it is due to the server program requesting it to listen.</p>
  <p>Here is a picture of the command prompt when I set the <code>&lt;port_number&gt;</code> to 15280:</p>
  <p><a href="https://i.stack.imgur.com/ZFdPQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZFdPQ.png" alt="image" /></a></p>
  <p>Is there a solution to this? I have checked many questions on SO and other websites, but none have given me the solution to this problem. They have only told me what the problem is, which I already know.</p>
  <p>The only change in the code is the definition of DEFAULT_PORT which I changed a few times to test different port numbers. I also changed the first parameter of the getaddrinfo to NULL, since I have no argc or argv values. I had some printf statements to figure out where the program was stopping but have removed them. Also, I do not have the for loop from the complete code, I put together the code snippets of the program which I walked through from start to finish.</p>
  <p>Server Code:</p>
  <pre><code>
  #include &lt;iostream&gt;
  #include &lt;WinSock2.h&gt;
  #include &lt;WS2tcpip.h&gt;
  #include &lt;Windows.h&gt;
  
  #pragma comment (lib, &quot;Ws2_32.lib&quot;)
  
  #define DEFAULT_PORT &quot;15280&quot;
  #define DEFAULT_BUFLEN 512
  
  int main()
  {
      WSADATA wsaData;
      int iResult;
      
      iResult = WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
      if (iResult != 0) {
          printf(&quot;WSAStartup failed: %d\n&quot;, iResult);
          return 1;
      }
  
      addrinfo *result = nullptr;
      addrinfo *ptr = nullptr;
      addrinfo hints;
  
      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;
  
      iResult = getaddrinfo(NULL, DEFAULT_PORT, &amp;hints, &amp;result);
      if (iResult != 0) {
          printf(&quot;getaddrinfo failed: %d\n&quot;, iResult);
          WSACleanup();
          return 1;
      }
  
      SOCKET ListenSocket = INVALID_SOCKET;
      ListenSocket = socket(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);
      if (ListenSocket == INVALID_SOCKET) {
          printf(&quot;Error at socket(): %d\n&quot;, WSAGetLastError());
          freeaddrinfo(result);
          WSACleanup();
          return 1;
      }
  
      iResult = bind(ListenSocket, result-&gt;ai_addr, (int)result-&gt;ai_addrlen);
      if (iResult == SOCKET_ERROR) {
          printf(&quot;bind failed with error: %d\n&quot;, WSAGetLastError());
          freeaddrinfo(result);
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      freeaddrinfo(result);
  
      if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR) {
          printf(&quot;Listen failed with error: %d\n&quot;, WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
      SOCKET ClientSocket;
      ClientSocket = INVALID_SOCKET;
      ClientSocket = accept(ListenSocket, nullptr, nullptr);
      if (ClientSocket == INVALID_SOCKET) {
          printf(&quot;accept failed: %d\n&quot;, WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
      char recvbuf[DEFAULT_BUFLEN];
      int iSendResult;
      int recvbuflen = DEFAULT_BUFLEN;
  
      do {
          iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
          if (iResult &gt; 0) {
              printf(&quot;Bytes printed: %d\n&quot;, iResult);
              iSendResult = send(ClientSocket, recvbuf, iResult, 0);
              if (iSendResult == SOCKET_ERROR) {
                  printf(&quot;send failed: %d\n&quot;, WSAGetLastError());
                  closesocket(ClientSocket);
                  WSACleanup();
                  return 1;
              }
              printf(&quot;Bytes sent: %d\n&quot;, iSendResult);
          }
          else if (iResult == 0) {
              printf(&quot;Connection closing...\n&quot;);
          }
          else {
              printf(&quot;recv failed: %d\n&quot;, WSAGetLastError());
              closesocket(ClientSocket);
              WSACleanup();
              return 1;
          }
      } while (iResult &gt; 0);
  
      iResult = shutdown(ClientSocket, SD_SEND);
      if (iResult == SOCKET_ERROR) {
          printf(&quot;shutdown failed: %d\n&quot;, WSAGetLastError());
          closesocket(ClientSocket);
          WSACleanup();
          return 1;
      }
      closesocket(ClientSocket);
      WSACleanup();
  
      return 0;
  }
  </code></pre>
  <p>Client Code:</p>
  <pre><code>
  #include &lt;iostream&gt;
  #include &lt;WinSock2.h&gt;
  #include &lt;WS2tcpip.h&gt;
  
  #pragma comment (lib, &quot;Ws2_32.lib&quot;)
  #pragma comment (lib, &quot;Mswsock.lib&quot;)
  #pragma comment (lib, &quot;AdvApi32.lib&quot;)
  
  #define DEFAULT_PORT &quot;15280&quot;
  #define DEFAULT_BUFLEN 512
  
  int main(int argc, char **argv)
  {
      WSADATA wsaData;
      int iResult;
      iResult = WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
      if (iResult != 0) {
          printf(&quot;WSAStartup failed: %d\n&quot;, iResult);
          return 1;
      }
      addrinfo* result = nullptr;
      addrinfo* ptr = nullptr;
      addrinfo hints;
      ZeroMemory(&amp;hints, sizeof(hints));
      hints.ai_family = AF_UNSPEC;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_protocol = IPPROTO_TCP;
      // first param is argv[1]
      iResult = getaddrinfo(NULL, DEFAULT_PORT, &amp;hints, &amp;result);
      if (iResult != 0) {
          printf(&quot;getaddrinfo failed: %d\n&quot;, iResult);
          WSACleanup();
          return 1;
      }
      SOCKET ConnectSocket = INVALID_SOCKET;
      ptr = result;
      ConnectSocket = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype, ptr-&gt;ai_protocol);
      if (ConnectSocket == INVALID_SOCKET) {
          printf(&quot;Error at socket(): %d\n&quot;, WSAGetLastError());
          freeaddrinfo(result);
          WSACleanup();
          return 1;
      }
      iResult = connect(ConnectSocket, ptr-&gt;ai_addr, (int)ptr-&gt;ai_addrlen);
      if (iResult == SOCKET_ERROR) {
          printf(&quot;Error: %d\n&quot;, WSAGetLastError());
          closesocket(ConnectSocket);
          ConnectSocket = INVALID_SOCKET;
      }
      freeaddrinfo(result);
      if (ConnectSocket == INVALID_SOCKET) {
          printf(&quot;Unable to connect to server!\n&quot;);
          WSACleanup();
          return 1;
      }
      int recvbuflen = DEFAULT_BUFLEN;
      const char *sendbuf = &quot;this is a test&quot;;
      char recvbuf[DEFAULT_BUFLEN];
      
      iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
      if (iResult == SOCKET_ERROR) {
          printf(&quot;send failed: %d\n&quot;, WSAGetLastError());
          closesocket(ConnectSocket);
          WSACleanup();
          return 1;
      }
      printf(&quot;Bytes Sent: %d\n&quot;, iResult);
      iResult = shutdown(ConnectSocket, SD_SEND);
      if (iResult == SOCKET_ERROR) {
          printf(&quot;shutdown failed: %d\n&quot;, WSAGetLastError());
          closesocket(ConnectSocket);
          WSACleanup();
          return 1;
      }
  
      do {
          iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
          if (iResult &gt; 0) {
              printf(&quot;Bytes received: %d\n&quot;, iResult);
          }
          else if (iResult == 0) {
              printf(&quot;Connection closed\n&quot;);
          }
          else {
              printf(&quot;recv failed: %d\n&quot;, WSAGetLastError());
          }
      } while (iResult &gt; 0);
  
      iResult = shutdown(ConnectSocket, SD_SEND);
      if (iResult == SOCKET_ERROR) {
          printf(&quot;shutdown failed: %d\n&quot;, WSAGetLastError());
          closesocket(ConnectSocket);
          WSACleanup();
          return 1;
      }
  
      closesocket(ConnectSocket);
      WSACleanup();
  
      return 0;
  }
  </code></pre>