title: TCP connection failure
body: <p>I’m having trouble with a simple TCP socket with the connect() and read() function. This program works if I start the server first, and the client second. However, when I start the client first, it continues to try to connect to the server, then when I start the server, it connects but the subsequent read returns 0 bytes. This suggests that the server closed the socket, but the debug statements do not support this. </p>
  
  <p>I’m trying to have the client continually trying to connect to the server on port 6380, then read data. If the connection is lost, the client will try to reconnect again.  I have SO_REUSEADDR set for the sockets. Is there something else that I'm missing?</p>
  
  <p>Source Code for Server</p>
  
  <pre><code>//***** SERVER *****
  #include &lt;iostream&gt;
  #include "SocketAdapter.h"
  #include "SocketServer.h"
  #include &lt;errno.h&gt;
  #include &lt;string.h&gt;
  
  unsigned char buffer[] = "123123123";
  
  int main()
  {
     SocketAdapter  socket;
     SocketServer   server(6380);
  
     server.Start();
     std::cout &lt;&lt; "Wait for Connection" &lt;&lt; std::endl;
     server.WaitForConnection(socket);
     std::cout &lt;&lt; "Start to Send Data" &lt;&lt; std::endl;
  
     while(1)
     {
        std::cout &lt;&lt; "Sending Data   ";
        int bytesSent = socket.Send(buffer, 10);
        std::cout &lt;&lt; "byteSent = " &lt;&lt; bytesSent &lt;&lt; std::endl;
        if (bytesSent == -1)
        {
           std::cout &lt;&lt; "Errno (" &lt;&lt; errno &lt;&lt; ") - " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
        }
  
        sleep(1);
     }//end while
  
     std::cout &lt;&lt; "Ending Server" &lt;&lt; std::endl;
     socket.Close();
  
     return 0;
  }
  </code></pre>
  
  <p>Source Code for Client</p>
  
  <pre><code>// ***** SIMPLE CLIENT ******
  #include &lt;iostream&gt;
  #include "SocketAdapter.h"
  #include "SocketClient.h"
  #include &lt;string.h&gt;
  #include &lt;errno.h&gt;
  
  int main()
  {
     SocketAdapter  socket;
     SocketClient   client(6380, "127.0.0.1");
  
     unsigned char buffer[1024];
  
  
     while(1)
     {
        client.Start();
  
        //Loop till we connect to Server
        while( !client.Connect(socket) )
        {
           std::cout &lt;&lt; "Trying to connect to server." &lt;&lt; std::endl;
           sleep(1);
        }
  
        std::cout &lt;&lt; "Connected to Server" &lt;&lt; std::endl;
  
        //Read till we get a timeout
        while(1)
        {
           int bytesRead = socket.Recv(buffer, 10,2000);
           if (bytesRead != 10)
           {
              std::cout &lt;&lt; "   Failed to Read" &lt;&lt; std::endl;
              std::cout &lt;&lt; "   bytesRead = " &lt;&lt; bytesRead &lt;&lt; std::endl;
              std::cout &lt;&lt; "   Errno (" &lt;&lt; errno &lt;&lt; ") - "
                        &lt;&lt; strerror(errno) &lt;&lt; std::endl;
              break;
           }
  
           std::cout &lt;&lt; "buffer = " &lt;&lt; buffer &lt;&lt; std::endl;
           usleep(250000);
        }
  
        std::cout &lt;&lt; "Something went wrong, restart" &lt;&lt; std::endl;
     }
  
     return 0;
  }
  </code></pre>
  
  <p>OUTPUT OF SERVER</p>
  
  <pre><code>./Server 
  Wait for Connection
  Start to Send Data
  Sending Data   byteSent = 10
  </code></pre>
  
  <p>OUTPUT OF CLIENT</p>
  
  <pre><code>./Client 
  Trying to connect to server.
  Trying to connect to server.
  Connected to Server
     Failed to Read
     bytesRead = 0
     Errno (107) - Transport endpoint is not connected
  Something went wrong, restart
  Close Socket
  Close Socket File Descriptor
  Connected to Server
  Trying to Recv on a closed socket
  Return Value = -1
  Errno        = Connection reset by peer
  
     Failed to Read
     bytesRead = -3
     Errno (104) - Connection reset by peer
  Something went wrong, restart
  Close Socket
  Close Socket File Descriptor
  Socket cannot shutdown!
  Return Value = -1
  Errno        = Transport endpoint is not connected
  
  Trying to connect to server.
  Close Socket
  Close Socket File Descriptor
  Socket cannot shutdown!
  Return Value = -1
  Errno        = Transport endpoint is not connected
  </code></pre>