title: Issue regarding accept() in socket server client programming in C (Linux)
body: <p>I have written a server and client program using socket in C in Linux.</p>
  
  <p>Codes are given below:</p>
  
  <p>Server Code:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;string.h&gt;    //strlen
  #include&lt;sys/socket.h&gt;
  #include&lt;arpa/inet.h&gt; //inet_addr
  #include&lt;unistd.h&gt;    //write
  
  int main(int argc , char *argv[])
  {
  int socket_desc , client_sock , c , read_size;
  struct sockaddr_in server , client;
  char client_message[2000];
  
  //Create socket
  socket_desc = socket(AF_INET , SOCK_STREAM , 0);
  if (socket_desc == -1)
  {
      printf("Could not create socket");
  }
  puts("Socket created");
  
  //Prepare the sockaddr_in structure
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons( 8888 );
  
  //Bind
  if( bind(socket_desc,(struct sockaddr *)&amp;server , sizeof(server)) &lt; 0)
  {
      //print the error message
      perror("bind failed. Error");
      return 1;
  }
  puts("bind done");
  
  //Listen
  listen(socket_desc , 3);
  
  //Accept and incoming connection
  puts("Waiting for incoming connections...");
  c = sizeof(struct sockaddr_in);
  
  //accept connection from an incoming client
  client_sock = accept(socket_desc, (struct sockaddr *)&amp;client, (socklen_t*)&amp;c);
  if (client_sock &lt; 0)
  {
      perror("accept failed");
      return 1;
   }
  puts("Connection accepted");
    close(client_sock);
    puts("I am at While loop");
    while(1)
      {
      }
  return 0;
  }
  </code></pre>
  
  <p>Client Code:</p>
  
  <pre><code>#include&lt;stdio.h&gt; //printf
  #include&lt;string.h&gt;    //strlen
  #include&lt;sys/socket.h&gt;    //socket
  #include&lt;arpa/inet.h&gt; //inet_addr
  
  int main(int argc , char *argv[])
  {
  int sock;
  struct sockaddr_in server;
  char message[1000] , server_reply[2000];
  
  //Create socket
  sock = socket(AF_INET , SOCK_STREAM , 0);
  if (sock == -1)
  {
      printf("Could not create socket");
  }
  puts("Socket created");
  
  server.sin_addr.s_addr = inet_addr("127.0.0.1");
  server.sin_family = AF_INET;
  server.sin_port = htons( 8888 );
  
  //Connect to remote server
  if (connect(sock , (struct sockaddr *)&amp;server , sizeof(server)) &lt; 0)
  {
      perror("connect failed. Error");
      return 1;
  }
  
  puts("Connected\n");
  close(sock);
  while(1);
  return 0;
  }
  </code></pre>
  
  <p>when I run server code and then client code it works fine but when I terminate client code run it again (server code executed once only) it automatically connects to server without accept being called by server.</p>
  
  <p>I don't want client to get connected until server calls  accept() to connect with client.</p>
  
  <p>I just want client to wait or give some timeout until server calls accept() to establish connection between them.</p>
  
  <p>Output I am getting is below :</p>
  
  <p>Server Output:</p>
  
  <pre><code>root@beaglebone:/home/Saad/test# ./server_test
  Socket created                                                      
  bind done                              
  Waiting for incoming connections...
  Connection accepted
  I am at While loop
  </code></pre>
  
  <p>Client Output:</p>
  
  <pre><code>root@beaglebone:/home/Saad/test# ./client_test     
  Socket created                         
  Connected                                                           
  
  ^C                                                   
  root@beaglebone:/home/Saad/test# ./client_test
  Socket created                                              
  Connected          
  </code></pre>
  
  <p>I don't know why this is happening and how to resolve it.</p>
  
  <p>UPDATE:</p>
  
  <p>I have tried the answer below after some edits in client code  I am facing same problem.  </p>
  
  <p>Client code:</p>
  
  <pre><code>#include&lt;stdio.h&gt; //printf
  #include&lt;string.h&gt;    //strlen
  #include&lt;sys/socket.h&gt;    //socket
  #include&lt;arpa/inet.h&gt; //inet_addr
  
  int main(int argc , char *argv[])
  {
     char buf[1];
  int sock;
  struct sockaddr_in server;
  char message[1000] , server_reply[2000];
  int ret;
  
  //Create socket
  sock = socket(AF_INET , SOCK_STREAM , 0);
  if (sock == -1)
  {
      printf("Could not create socket");
  }
  puts("Socket created");
  
  server.sin_addr.s_addr = inet_addr("192.168.1.2");
  server.sin_family = AF_INET;
  server.sin_port = htons( 8888 );
  
  //Connect to remote server
  ret = connect(sock , (struct sockaddr *)&amp;server , sizeof(server));
  if (ret &lt; 0)
  {
      perror("connect failed. Error");
      return 1;
  }
  recv(sock, buf, 1, MSG_WAITFORONE);
  printf("%s",buf);
  puts("Connected\n");
  close(sock);
  sock = socket(AF_INET , SOCK_STREAM , 0);
  if (sock == -1)
  {
      printf("Could not create socket");
  }
  puts("Socket created");
  ret = connect(sock , (struct sockaddr *)&amp;server , sizeof(server));
  if (ret &lt; 0)
  {
      perror("connect failed. Error");
      return 1;
  }
  else
  {
   puts("Connected\n");
  }
  while(1);
  return 0;
  }
  </code></pre>
  
  <p>Client output:</p>
  
  <pre><code>root@beaglebone:/home/Saad/test# ./client_test_1
  Socket created
  1Connected
  
  Socket created
  Connected
  </code></pre>