title: server client chat program. Displaying online users
body: <p>I'm trying to display the online users in a server to a client. I've declared a client_struct which stores the necessary information when a client connects.
  I'm also forking a child process everytime a user is authenticated and wishes to communicate to the server.
  However when a second user connects to the server, the client side program just displays 1 currently online user and stops working.
  Any reason? Been hacking at the code for 4 hours in a row now. </p>
  
  <p>Server</p>
  
  <pre><code>/*
      C socket server example
  */
  
  #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
  struct client_struct{
      char ip[20];
      char username[20];
      char password[20];
      int busy;
      int online;
      int user_number;
  
  };
  struct client_struct client_database[25];
  char users_online[20][20];
  int user_count=0;
  int main(int argc , char *argv[])
  {
      int socket_desc , client_sock , c , read_size,i,j=0,not_valid=1,l;
      struct sockaddr_in server , client;
      char client_message[2000],username_database[25][25],password_database[25][25],str[INET_ADDRSTRLEN],username[20],password[20],garbage[20];
      unsigned long clients[25];
  
      strcpy(client_database[0].username,"rushabh");
      strcpy(client_database[0].password,"nagda");
      //client_database[0].busy=0;
      //client_database[0].online=0;
      //client_database[0].user_number=0;
      strcpy(client_database[1].username,"sahil");
      strcpy(client_database[1].password,"gill");
      //client_database[1].busy=0;
      //client_database[1].online=0;
      //client_database[1].user_number=0;
      strcpy(client_database[2].username,"panty");
      strcpy(client_database[2].password,"avi");
      //client_database[2].busy=0;
      //client_database[2].online=0;
      //client_database[2].user_number=0;
  
      //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
      while(1)
      {
          client_sock = accept(socket_desc, (struct sockaddr *)&amp;client, (socklen_t*)&amp;c);
          //Incrementing usercount
          inet_ntop(AF_INET, &amp;(client.sin_addr), str, INET_ADDRSTRLEN); //extracting ip address
          printf("%s\n", str); // prints "192.0.2.33"
          //if(fork()==0) //remember that this is wrong!
          //{
              //printf("%u\n",client.sin_addr.s_addr);
              //clients[user_count]=client.sin_addr.s_addr;
  
              if (client_sock &lt; 0)
              {
                  perror("accept failed");
                  return 1;
              }
              puts("Connection accepted");
              //User authentication
              write(client_sock,"Please enter username:",strlen("Please enter username\n"));
              recv(client_sock,username,20,0);
  
              write(client_sock,"Please enter password:",strlen("Please enter password\n"));
              recv(client_sock,password,20,0);
  
              //Authentication
              for(j=0;j&lt;3;j++)
              {
                  //printf("%s %s %s %s\n",client_database[j].username,username,client_database[j].password,password);
                  if(!(strcmp(client_database[j].username,username)) ||
                      !(strcmp(client_database[j].password,password)))
                  {
                      printf("yay!\n");
                      write(client_sock,"Authenticated\n",strlen("Authenticated\n"));
                      recv(client_sock,garbage,20,0);
                      client_database[j].online=1;
                      client_database[j].busy=0;
                      strcpy(client_database[j].ip,str);
                      not_valid=0;
                      break;
  
                  }
  
              }   
  
              if(not_valid==1)
              {
                  write(client_sock,"Invalid username or password\n",strlen("Invalid username or password\n"));
                  recv(client_sock,garbage,20,0);
                  return(0);
              }
  
              if(fork()==0)
              {
                  write(client_sock,"The online users are",strlen("The online users are"));
                  recv(client_sock,garbage,20,0);
                  for(l=0;l&lt;20;l++)
                  {
                      if(client_database[l].online==1)
                      {
                          write(client_sock,client_database[l].username,strlen(client_database[l].username));
                          recv(client_sock,garbage,20,0);
                      }
                      else
                      {
                          printf("%s %d\n",client_database[l].username,client_database[l].online);
                          write(client_sock,"garbage",strlen("garbage"));
                          recv(client_sock,garbage,20,0);
                      }
                  }
  
                  //Receive a message from client
  
                  while( (read_size = recv(client_sock , client_message , 2000 , 0)) &gt; 0 )
                  {
                      //Send the message back to client
                      write(client_sock , client_message , strlen(client_message));
                      puts(client_message);
                      memset(&amp;client_message[0], 0, sizeof(client_message));
  
                  }
  
                  if(read_size == 0)
                  {
                      puts("Client disconnected");
                      fflush(stdout);
                  }
                  else if(read_size == -1)
                  {
                      perror("recv failed");
                  }
              }
          //}
      }
      return 0;
  }
  </code></pre>
  
  <p>Client</p>
  
  <pre><code>/*
      C ECHO client example using sockets
  */
  #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,i=0,username_counter=0;
      struct sockaddr_in server;
      char message[1000] , server_reply[2000],server_reply1[2000];
  
      //Create socket
      sock = socket(AF_INET , SOCK_STREAM , 0);
      printf("%d\n",sock);
      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");
  
      while(1)
      {
          recv(sock,server_reply,2000,0); //rec username
          puts(server_reply);
          scanf("%s",message);
          send(sock,message,strlen(message),0);
          memset(&amp;server_reply[0], 0, sizeof(server_reply));
  
          recv(sock,server_reply,2000,0); //recv password
          puts(server_reply);
          scanf("%s",message);
          send(sock,message,strlen(message),0);
          memset(&amp;server_reply[0], 0, sizeof(server_reply));
  
          recv(sock,server_reply,2000,0); //auth
          puts(server_reply);
          send(sock,"foo",strlen("foo"),0);
          memset(&amp;server_reply[0], 0, sizeof(server_reply));
  
          recv(sock,server_reply,2000,0); //online users
          puts(server_reply);
          send(sock,"foo",strlen("foo"),0);
          memset(&amp;server_reply[0], 0, sizeof(server_reply));
  
          while(1)
          {
              if(username_counter==20)
              {
                  break;
              }
              recv(sock,server_reply,2000,0);
              if(strcmp(server_reply,"garbage"))
                  puts(server_reply);
              memset(&amp;server_reply[0], 0, sizeof(server_reply));
              send(sock,"foo",strlen("foo"),0);
              username_counter++;
          }
  
          /*memset(&amp;server_reply[0], 0, sizeof(server_reply));
          recv(sock,server_reply,2000,0);
          puts(server_reply);
          memset(&amp;server_reply[0], 0, sizeof(server_reply));
          recv(sock,server_reply,2000,0);
          puts(server_reply);*/
          break;
      }
      printf("Authenticated\n");
      memset(&amp;server_reply[0], 0, sizeof(server_reply));
      //keep communicating with server
      while(1)
      {
          printf("Enter message : ");
          scanf("%s" , message);
  
          //Send some data
          if( send(sock , message , strlen(message) , 0) &lt; 0)
          {
              puts("Send failed");
              return 1;
          }
  
          //Receive a reply from the server
          if( recv(sock , server_reply , 2000 , 0) &lt; 0)
          {
              puts("recv failed");
              break;
          }
  
          puts("Server reply :");
          puts(server_reply);
  
          memset(&amp;server_reply[0], 0, sizeof(server_reply));
      }
  
      close(sock);
      return 0;
  }
  </code></pre>