title: C++ Reconnect TCP Server to Client after Client restart
body: <p>I want to implement a 'flexible' TCP connection where I can randomly shutdown and restart both the Server and the Client. The other one should then automatically detect the shutdown and enter try to reconnect. I successfully implemented this s.t. I can shutdown and restart the server. The client discovers the shutdown (via <code>recv(...) == 0</code>) and then closes the connection (it therefore closes the sockets <code>close(this-&gt;sockfd_)</code> and <code>close(this-&gt;newsockfd_)</code>).</p>
  
  <p>Unfortunately I am not able to get this working the other way around. I initialize the server (using the class constructor) the following way:</p>
  
  <pre><code>tcpServer::tcpServer(int _port) {
    this-&gt;sockfd_ = -1;
    this-&gt;port_ = _port;
    this-&gt;connected_ = false;
    if ((this-&gt;sockfd_ = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
      this-&gt;dieWithError("ERROR opening Socket");
    else
      printf("-&gt; Port %d: Socket opened\n", this-&gt;port_);
    // get rid of "address already in use" error message
    int yes = 1;
    setsockopt(this-&gt;sockfd_, SOL_SOCKET, SO_REUSEADDR, &amp;yes, sizeof(int));
    /* assign values to the fields of struct sockaddr_in */
    bzero((char *) &amp;this-&gt;serv_addr_, sizeof(this-&gt;serv_addr_));
    this-&gt;serv_addr_.sin_family = AF_INET;
    this-&gt;serv_addr_.sin_port = htons(this-&gt;port_);
    this-&gt;serv_addr_.sin_addr.s_addr = INADDR_ANY;
    /* bind the socket to an address */
    if (bind(this-&gt;sockfd_, (struct sockaddr *) &amp;this-&gt;serv_addr_, sizeof(this-&gt;serv_addr_)) &lt; 0) {
      printf("-&gt; Port %d:", this-&gt;port_);
      this-&gt;dieWithError("ERROR on binding");
    }
    printf("-&gt; Binding successful. Start TCP client in new terminal\n");
    fflush(stdout);
    /* listen for connections and accept a connection */
    listen(this-&gt;sockfd_, 5);
    this-&gt;clilen_ = sizeof(this-&gt;cli_addr_);
    if ((this-&gt;newsockfd_ = accept(this-&gt;sockfd_, (struct sockaddr *) &amp;this-&gt;cli_addr_, &amp;this-&gt;clilen_)) &lt; 0)
      this-&gt;dieWithError("Error on accept");
    else {
      printf("-&gt; Connection established\n");
      this-&gt;connected_ = true;
    }
  }
  </code></pre>
  
  <p>So once the server detects that the connection is closed, it enters a loop where it tries to reconnect using the following code:</p>
  
  <pre><code>void tcpServer::connect() {
    if (this-&gt;sockfd_ == -1) {
      /* create socket */
      if ((this-&gt;sockfd_ = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
          this-&gt;dieWithError("ERROR opening Socket");
      else
          printf("-&gt; Port %d: Socket opened\n", this-&gt;port_);
      // get rid of "address already in use" error message
      int reuse_address = 1;
      setsockopt(this-&gt;sockfd_, SOL_SOCKET, SO_REUSEADDR, &amp;reuse_address, sizeof(int));
      /* listen for connections and accept a connection */
      listen(this-&gt;sockfd_, 5);
      this-&gt;clilen_ = sizeof(this-&gt;cli_addr_);
      if ((this-&gt;newsockfd_ = accept(this-&gt;sockfd_, (struct sockaddr *) &amp;this-&gt;cli_addr_, &amp;this-&gt;clilen_)) &lt; 0)
          this-&gt;dieWithError("Error on accept");
      else {
          printf("-&gt; Connection established\n");
          this-&gt;connected_ = true;
      }
    }
  }
  </code></pre>
  
  <p>Some simple debugging output tells me that in the reconnect-mode, the server gets stuck in the 
  <code>accept(this-&gt;sockfd_, (struct sockaddr *) &amp;this-&gt;cli_addr_, &amp;this-&gt;clilen_)</code> call. Another observation I made is that the client does not shut down properly (via <code>ctrl-c</code>), i.e., it gets stuck in a loop somewhere and does not properly close the connection.</p>
  
  <p>Since I am a total beginner with the TCP stuff, I would be very happy if someone could point me to the right direction. Thank you.</p>