title: Why did my winsocks2 send result in 10057 error sometimes?
body: <p>I did tcp socket connection with normal(?) steps as the code shows below. But send <strong>sometimes</strong> result in <strong>10057</strong> error while server-end printed some log which says connection is in, I have no idea about what's wrong with my code. Any help is appreciated.
   Any information about stable and robust windows socket programming is appreciated.</p>
  
  <p>MSDN description about 10057 error:</p>
  
  <blockquote>
    <p>Socket is not connected.A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using sendto) no address was supplied. Any other type of operation might also return this error—for example, setsockopt setting SO_KEEPALIVE if the connection has been reset.</p>
  </blockquote>
  
  <hr>
  
  <p>PS: Real ip address in below code snippet is replaced by <em>127.0.0.1</em>. Code isn't runnable.</p>
  
  <pre><code>int connect_to_server() {
  SOCKET soc;
  SOCKADDR_IN serveraddr;
  SOCKADDR_IN clientaddr;
  unsigned char buf[1024];
  
  WSADATA wsa;
  WSAStartup(MAKEWORD(2, 2), &amp;wsa);
  
  /* create a tcp socket; */  
  if ((soc = socket(AF_INET, SOCK_STREAM, 0/*IPPROTO_TCP*/)) &lt;= 0)
  {
      LOGFMTF("errcode[-1], create socket fail!");
      return -1;
  }
  
  serveraddr.sin_family = AF_INET;
  
  serveraddr.sin_port = htons(9102);
  serveraddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
  
  unsigned long mode = 1;  // 1 to enable non-blocking socket
  if (ioctlsocket(soc, FIONBIO, &amp;mode) == SOCKET_ERROR) {
      LOGFMTF("errcode[-2], ioctlsocket fail.");
      return -2;
  }
  
  if (::connect(soc, (SOCKADDR *)&amp;serveraddr, sizeof(serveraddr)) != 0)
  {
      /* FIONBIO socket enters here can go run; non-FIONBIO socket enters here stands for real error;*/
      if (WSAGetLastError() != WSAEWOULDBLOCK) {
          LOGFMTF("errcode[-3], connect fail.");
          //return -3;
      }
  
      //return -1;
  }
  
  g_client_fd = soc;
  
  LOGFMTT("client_fd[%d]", g_client_fd);
  LOGFMTT("connect to server last err[%d].", WSAGetLastError());
  
  return 0;
  }
  
  /** send loop; */
  DWORD WINAPI send_thread_loop(LPVOID pM) {
  LOGFMTT("send thread[%d] start.", GetCurrentThreadId());
  
  for (;!g_to_exit;) 
  {
      QMutexLocker locker(&amp;g_send_queue_lock);
      if (g_send_queue.count() == 0) {
          Sleep(Send_Thread_Gap);
      }else {
          LOGFMTT("send one packet.");
          int pakcet_size = g_send_queue.begin()-&gt;size;
          char *addr = g_send_queue.begin()-&gt;data;
          ::send(g_client_fd, addr, pakcet_size, 0);
  
          /* ------------!Attention begin------------ */
          LOGFMTT("send last err[%d].", GetLastError());--------&gt; sometimes got 10057 error.
          /* ------------!Attention end------------ */
  
          delete[]addr;
          g_send_queue.erase(g_send_queue.begin());
      }
  }
  
  LOGFMTT("send thread[%d] exit.", GetCurrentThreadId());
  
  return 0;
  }
  
  int start_net_thread() {
  g_recv_thread = CreateThread(NULL, 0, recv_thread_loop, NULL, 0, NULL);
  
  g_send_thread = CreateThread(NULL, 0, send_thread_loop, NULL, 0, NULL);
  
  return 0;
  }
  
  int main(){
  if(connect_to_server() != 0) 
  {
      QMessageBox msgBox;
      msgBox.setText("failed.");
      msgBox.exec();
      return -1;
  }
  
  start_net_thread();
  
  return 0;
  }
  </code></pre>