title: How to avoid TIME_WAIT for server sockets?
body: <p>I know you will mask this as duplicate (<a href="https://stackoverflow.com/questions/5592747/bind-error-while-recreating-socket">question1</a>, <a href="https://stackoverflow.com/questions/1241144/socket-remains-open-after-program-has-closed-c">question2</a>, <a href="https://stackoverflow.com/questions/10747272/tcp-socket-close-and-rebind">question3</a>) but the answers are not what I'm looking for (and I think also other people). <br>
  So, I'm refering to socket masters (I love you guys): how can I get bind error(address already in use) if I close the socket? <br>
  I will describe my problem.</p>
  
  <blockquote>
    <p>I've got a client that communicate with a <strong>server</strong> <br>
    In the server, I have two sockets: sockS (the main socket, that listens) and sockTX (the client one)<br>
    If I call my programs once, the communication is fine, then I close <strong>both</strong> sockets <br>
    If I recall server and client, I get the error and I've to wait the TIME_WAIT (~3 minutes seconds on Ubuntu 32bit)</p>
  </blockquote>
  
  <p>Why, after closing <strong>both</strong> sockets I still get bind error? <br> 
  There's a way to make it works without any magic (SO_REUSEADDR)? <br>
  I know something in my code is wrong... <br><br>
  Thank you everyone.<br> <br>
  <strong>That's the code</strong>: <br>
  Client<br></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;string.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  #define PORT 5000
  #define SERVER "127.0.0.1"
  #define MAXLINE 128
  
  int printMessage(char* str);
  
  int main(){
  char buff[MAXLINE+1];
  
  struct sockaddr_in server, client;
  struct hostent *host;
  int sock, n;
  //socklen_t len;
  
      if((sock = socket(AF_INET,SOCK_STREAM,0)) == -1){
          perror("\nErrore socket()");
          return -1;
      }
  
      client.sin_family = AF_INET;
      client.sin_port = htons(0); // la porta e' scelta dal sistema operativo
      client.sin_addr.s_addr = htonl(INADDR_ANY);
  
      if( bind(sock,(struct sockaddr*)&amp;client, sizeof(client)) == -1){
          perror("\nErrore bind()");
          return -1;
      }
  
      server.sin_family = AF_INET;
      server.sin_port = htons(PORT);
  
      if((host = gethostbyname(SERVER)) == NULL ){
          perror("\nErrore gethostbyname()");
          return -1;
      }
  
      server.sin_addr = *((struct in_addr *)host-&gt;h_addr);
  
      if(connect(sock, (struct sockaddr*)&amp;server, sizeof(server)) &lt; 0){
          perror("\nErrore connect");
          return -1;
      }
  
      // MESSAGGIO
  
      sprintf(buff, "CTX\nclientTX\nserver\nCiao da client\n");
      if(send(sock, buff, strlen(buff), 0) &lt; 0) {
          perror("\nErrore sendto");
          return -1;
      }
      else {
          printf("\nMessaggio inviato");
      }
  
      if((n = recv(sock, buff, MAXLINE, 0)) &lt; 0) {
          perror("\nErrore ricezione risposta");
          return -1;
      } else {
          buff[n] = '\0';
          int test = printMessage(buff);
          printf("\nEsito: %s\n", (test == 1 ? "OK" : "FAIL"));
      }
  
      shutdown(sock, 2); // 2 = RD_WR
      close(sock);
      return 0;
  }
  
  int printMessage(char* str){
  int i;
  char* temp;
  
      printf("Mittente: ");
      for(i = 0; str[i] != '\n'; i++)
          printf("%c", str[i]);
      printf(" ");
      for(i = i+1; str[i] != '\n'; i++)
          printf("%c", str[i]);
      printf("\nDestinatario: ");
      for(i = i+1; str[i] != '\n'; i++)
          printf("%c", str[i]);
  
      temp = (char*)malloc(30 * sizeof(char));
      strncpy(temp, str+i+1, 30);
      printf("Messaggio: %s\n", temp);
  
      if(strcmp(temp, "OK") == 0)
          return 1;
      else
          return 0;
  }
  </code></pre>
  
  <p><br>
  Server:<br></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netdb.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  #define PORT 5000
  #define SERVER "127.0.0.1"
  #define MAXLINE 128
  
  int printMessage(char* str);
  
  int main() {
  char buff[MAXLINE+1];
  struct sockaddr_in server; //, client;
  int sockS, sockTX, n;
  
      if((sockS = socket(AF_INET,SOCK_STREAM,0)) == -1)
      {
          perror("\nErrore socket()");
          return -1;
      }
  
      server.sin_family = AF_INET;
      server.sin_port = htons(PORT);
      server.sin_addr.s_addr = htonl(INADDR_ANY);
  
      if(bind(sockS, (struct sockaddr *)&amp;server, sizeof(server)) == -1)
      {
          perror("\nErrore bind()");
          return -1;
      }
  
      if(listen(sockS, 10) == -1)
      {
          perror("\nErrore listen()");
          return -1;
      }
      printf("SERVER\nInizializzazione completata!\n");
  
      if((sockTX = accept(sockS, (struct sockaddr *)NULL, NULL)) == -1)
      {
          perror("\nErrore accept()");
          return -1; 
      }
      printf("Socket connesso\n");
  
      // INVIO
      if((n = recv(sockTX, buff, MAXLINE, 0)) &lt; 0) {
          perror("\nErrore recv()");
          return -1; // BREAK??
  
      } else {
          buff[n] = '\0';
          printMessage(buff);
  
      }
  
      sprintf(buff, "S\nserver\nclientTX\nOK\n");
      if(send(sockTX, buff, strlen(buff), 0) &lt; 0){
          perror("\nErrore send()");
          return -1; // BREAK??
  
      } else {
          printf("Risposta inviata\n");
  
      }
  
      shutdown(sockTX, 2);
      close(sockTX);
  
      shutdown(sockS, 2);
      close(sockS);
      return 0;   
  }
  
  int printMessage(char* str){
  int i;
  char* temp;
  
      printf("Mittente: ");
      for(i = 0; str[i] != '\n'; i++)
          printf("%c", str[i]);
      printf(" ");
      for(i = i+1; str[i] != '\n'; i++)
          printf("%c", str[i]);
      printf("\nDestinatario: ");
      for(i = i+1; str[i] != '\n'; i++)
          printf("%c", str[i]);
  
      temp = (char*)malloc(30 * sizeof(char));
      strncpy(temp, str+i+1, 30);
      printf("Messaggio: %s\n", temp);
  
      if(strcmp(temp, "OK\n") == 0)
          return 1;
      else
          return 0;
  }
  </code></pre>
  
  <p>Thank you everyone <br><br></p>
  
  <p><strong>EDIT 1</strong>: Possible solution (not very beautiful, but just a bit more than SOCK_REUSEADDR)<br>
  Try to add, before shutdown and close of the two server's socket, a sleep(1). <br>
  The client will close before the server and it will work<br>
  Not very beautiful, I know. <br><br>
  Or, better, before closing the first socket inside the server, you can check if the client closed the connection (like <a href="https://stackoverflow.com/questions/12402549/check-if-socket-is-connected">here</a>)</p>