title: Winsock not sending in a while loop
body: <p>I am very new to networking and have an issue with sending messages during a while loop.</p>
  
  <p>To my knowledge I should do something along the lines of this:</p>
  
  <pre><code>Create Socket()
  Connect()
  
  While
       Do logic
       Send()
  End while
  
  Close Socket()
  </code></pre>
  
  <p>However it sends once and returns -1 there after.</p>
  
  <p>The code will only work when I create the socket in the loop.</p>
  
  <pre><code>While
       Create Socket()
       Connect()
       Do logic
       Send()
       Close Socket()
  End while
  </code></pre>
  
  <p>Here is a section of the code I am using but doesn't work:</p>
  
  <pre><code>//init winsock
  WSAStartup(MAKEWORD(2, 0), &amp;wsaData);
  
  //open socket
      sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  
      //connect
      memset(&amp;serveraddr, 0, sizeof(serveraddr));
      serveraddr.sin_family      = AF_INET;
      serveraddr.sin_addr.s_addr = inet_addr(ipaddress);
      serveraddr.sin_port        = htons((unsigned short) port);
      connect(sock, (struct sockaddr *) &amp;serveraddr, sizeof(serveraddr));
  
  while(true) {
  
      if (send(sock, request.c_str(), request.length(), 0)&lt; 0 /*!= request.length()*/) {
          OutputDebugString(TEXT("Failed to send."));
      } else {
          OutputDebugString(TEXT("Activity sent."));
      }
      Sleep(30000);
  }
  //disconnect
  closesocket(sock);
  
  //cleanup
  WSACleanup();
  </code></pre>
  
  <p>The function CheckForLastError() returns:10053</p>
  
  <p>WSAECONNABORTED
  Software caused connection abort.
  An established connection was aborted by the software in your host computer, possibly due to a data transmission time-out or protocol error</p>
  
  <p>Thanks</p>