title: Error 10054 in socket programming
body: <p>I'm writing a socket programming code in C and I'm running a client and a server on the same computer.here is the code for server</p>
  
  <pre><code>  int main()
  {
      WSADATA wsa;
      if (WSAStartup(MAKEWORD(2, 2), &amp;wsa) != 0)
      {
          cout &lt;&lt; "WSAStartup failed!" &lt;&lt; endl;
          return -1;
      }
  
      SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      if (sock == INVALID_SOCKET)
      {
          cout &lt;&lt; "socket failed with error:" &lt;&lt; WSAGetLastError() &lt;&lt; endl;
  
          WSACleanup();
          return -1;
      }
  
      struct sockaddr_in service;
      service.sin_addr.s_addr = inet_addr("127.0.0.1");
      service.sin_family = AF_INET;
      service.sin_port = htons(5059);
  
      if (bind(sock, (SOCKADDR *)&amp;service, sizeof(service)) == SOCKET_ERROR)
      {
          cout &lt;&lt; "bind failed with error:" &lt;&lt; WSAGetLastError() &lt;&lt; endl;
  
          closesocket(sock);
          WSACleanup();
          return -1;
      }
  
      if (listen(sock, 5) == SOCKET_ERROR)
      {
          cout &lt;&lt; "listen failed with error:" &lt;&lt; WSAGetLastError() &lt;&lt; endl;
  
          closesocket(sock);
          WSACleanup();
          return -1;
      }
      while (true)
      {
          cout &lt;&lt; "Waiting for a PKCS11 client ..." &lt;&lt; endl;
  
          SOCKET accsock = accept(sock, NULL, NULL);
          if (accsock == INVALID_SOCKET)
          {
              cout &lt;&lt; "accept failed with error:" &lt;&lt; WSAGetLastError() &lt;&lt; endl;
  
              closesocket(sock);
              WSACleanup();
              return -1;
          }
  
          cout &lt;&lt; "PKCS11 client connected ..." &lt;&lt; endl;
  
          while (true)
          {
              int receive_size = 0;
              char test[20];
              receive_size = recv(accsock, test, 20, 0);
              if (receive_size == SOCKET_ERROR)
              {
                  cout &lt;&lt; endl &lt;&lt; "ERROR : "&lt;&lt;WSAGetLastError() &lt;&lt; " PKCS11 client disconnected." &lt;&lt; endl;
                  closesocket(accsock);
                  break;
              }
  
              test[receive_size] = '\0';
              command = temp;
              printf("recieved value is : %s\n",test);
            }
              }
  </code></pre>
  
  <p>and here is the code for client</p>
  
  <pre><code>int main(){
      /*CK_SESSION_HANDLE hSession = NULL;
      CK_MECHANISM_PTR pMechanism = NULL;
      CK_OBJECT_HANDLE hKey = NULL;
      C_EncryptInit(hSession, pMechanism, hKey);*/
  
      char temp[10];
      printf("initialising ... \n");
      if (WSAStartup(MAKEWORD(2, 2), &amp;wsa) != 0)
      {
          printf("Error = :%d", WSAGetLastError());
          WSACleanup();
          return -1;
      }
      printf("initialised\n");
      //create a socket
      if ((s = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
      {
          printf("Could not create socket : %d", WSAGetLastError());
          WSACleanup();
          return -1;
      }
  
      printf("Socket created.\n");
  
  
      server.sin_addr.s_addr = inet_addr("127.0.0.1");
      server.sin_family = AF_INET;
      server.sin_port = htons(5059);
  
      //Connect to remote server
      if (connect(s, (struct sockaddr *)&amp;server, sizeof(server)) &lt; 0)
      {
          puts("connect error");
          return 1;
      }
  
      puts("Connected");
  
  
      _itoa(CK_ENCRYPT, temp, 16);
      command = temp;
      int counter = 0;
      char* test = "H";
      while (counter != strlen(test)) {
      counter += send(s, test, strlen(test), 0);
      cout &lt;&lt; counter &lt;&lt; endl;
  }   
  
  
      //while (1);
      return 0;
  }
  </code></pre>
  
  <p>The problem is that when I try to send more than one byte from client to server I get 10054 Error unless I uncomment the while(1) line which in that case I recieve the massage properly in the server.does anyone have any Idea what's wrong with this code?</p>