title: socket send() hangs after sending certain amount of bytes
body: <p>I want to send a string to my nodejs TCP server.</p>
  
  <p>This is the code for my client (C++):</p>
  
  <pre><code>#include &lt;nds.h&gt;
  #include &lt;dswifi9.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;string&gt;
  
  std::string hostname = "hostname";
  int port = 61733;
  int sock;
  
  int main(){
      Connect(); //function to connect with a WiFi network
      ConnectToServer();
  
      unsigned long lastSendTime = 0;
      unsigned long sendInterval = 200; //send a message every 200 milliseconds
      unsigned int nMsgSent = 0;
  
      while (1){
          unsigned long now = milliseconds(); //function to get the time in milliseconds
          if ((now - lastSendTime) &gt; sendInterval){
              std::string request = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
              request += request;
              request += std::to_string(nMsgSent);
              request += "\n";
  
              int sendResult = ::send(sock, request.c_str(), request.length(), 0);
              iprintf("req(%d): %d\n", nMsgSent, sendResult);
  
              nMsgSent++;
              lastSendTime = now;
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>This is my server code (JS):</p>
  
  <pre><code>var net = require('net');
  
  var tcpServer = net.createServer();  
  var tcpPort = 61733;
  
  tcpServer.on('connection', onConnection);
  
  function onConnection(conn){  
    var remoteAddress = conn.remoteAddress + ':' + conn.remotePort;
    console.log('New client connection from: %s', remoteAddress);
  
    conn.on('data', onData);
    conn.once('close', onClose);
    conn.on('error', onError);
  
    function onData(data){
      console.log('connection data from %s: %s', remoteAddress, data);
    }
  
    function onClose(){
      console.log('connection from %s closed', remoteAddress);
    }
  
    function onError(err){
      console.log('Connection %s error: %s', remoteAddress, err.message);
    }
  }
  
  tcpServer.listen(tcpPort, function(){  
    console.log('Server listening on: %j', tcpServer.address());
  });
  </code></pre>
  
  <p>Here comes the problem:</p>
  
  <p>The client only sends about 180 messages and then <code>send</code> hangs. The server only receives ~120 messages. When I increase the length of the request, the client sends less requests and when I decrease the length, the clients sends more requests. I also found out that when I decrease the sendInterval, I can send more requests before <code>send</code> hangs. I don't know what I'm doing wrong. When I execute similar code with a nodejs client, everything is okay, so the problem lies with the C++ client.</p>
  
  <p>EDIT:</p>
  
  <p>I don't really think this is necessary, but here is the code of <code>connectToServer()</code>:</p>
  
  <pre><code>void ConnectToServer()
  {
      sock = socket(AF_INET, SOCK_STREAM, 0);
      if (socket &gt;= 0)
      {
          printf("Created Socket!\n");
      }
      else
      {
          printf("Couldn't create socket");
      }
  
      struct hostent * host = gethostbyname(hostname.c_str());
      if (host != NULL)
      {
          printf("Found IP Address!\n");
      }
      else
      {
          printf("IP not found");
      }   
  
      struct sockaddr_in server;
      server.sin_family = AF_INET;
      server.sin_port = htons(port);
      server.sin_addr.s_addr = *((unsigned long *)(host-&gt;h_addr_list[0]));
  
      int connectResult = ::connect(sock, (struct sockaddr *)&amp;server, sizeof(server));
      if (connectResult &gt;= 0)
      {
          printf("Connected to server!\n");
      }
      else
      {
          printf("Not connected to server. Err: %d\n", connectResult);
      }   
  }
  </code></pre>
  
  <p>Output from C++ client(send interval: 2000ms, msgLength: 126-127 chars):</p>
  
  <pre><code>req(1): 126
  req(2: 126
  req(3): 126
  ...keeps going on for a while...
  req(9): 126
  req(10): 126
  req(11): 127
  req(12): 127
  ...keeps going on for a while...
  req(74): 64
  send() hangs after this
  </code></pre>
  
  <p>Output from nodejs server(only receives 9 messages):</p>
  
  <pre><code>connection data from ip::port: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345678900
  
  ...keeps going on for a while...
  
  connection data from ip::port: abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345678909
  </code></pre>
  
  <p>Test results to see if there's a connection between send interval, message length and the number of sent messages:</p>
  
  <pre>
   sendInterval |  msgLength  |   nMsg
  ---------------------------------------
       200      |    127      |    26
       200      |    65       |    33
  ---------------------------------------
       400      |    65       |    59
       400      |    127      |    53
  ---------------------------------------
       2000     |    65       |    47
       2000     |    127      |    21
  </pre>
  
  <p>(I don't see a connection between these values)</p>