title: Receiving multiple client packets on one server
body: <p>I have 3 applications, one server, 2 clients. I am trying to program this so that I can send a log in request from my clients to the server and request to view information about another client. </p>
  
  <p>Questions: i am using the same ip and port # for all 3 of the applications (it's all done on 3 visual studio applications all on my computer) otherwise I don't think it connects to the server. is this ok?</p>
  
  <p>When I try and use the listen and accept outside of the while loop in the server, it works for one client but not the other. When I try and put the listen and accept inside the while loop it works for both clients but does not respond to more than one accept. What is my issue?</p>
  
  <p>Server</p>
  
  <pre><code>struct Users {
  
  int message;
  char userName[50];
  char ipAddress[50];
  int PortNumber;
  bool online;
  
  };
  
  int main () {
  
  //Create users
  Users client[2];
  
  client[0].PortNumber = 20000;
  client[0].online = false;
  sprintf(&amp;client[0].userName[0], "Albert");
  sprintf(&amp;client[0].ipAddress[0], "127.0.0.1");
  
  client[1].PortNumber = 20000;
  client[1].online = false;
  sprintf(&amp;client[1].userName[0], "Monique");
  sprintf(&amp;client[1].ipAddress[0], "127.0.0.1");
  
  
  //Set up all the connection stuff
  struct sockaddr_in SvrAddr;
  SOCKET WelcomeSocket, ConnectionSocket;
  int PortNumber = 20000;
  int result;
  char IPAddress[] = "127.0.0.1";
  char RxBuffer[128];
  char TxBuffer[128];
  
  
  WORD wVersionRequested; 
  WSADATA wsaData; 
  
  wVersionRequested = MAKEWORD(2,2); //For Server 
  
  
  if(WSAStartup(wVersionRequested, &amp;wsaData) != 0) 
      return -1;
  
  
  //Setting up the welcome socket
  WelcomeSocket = socket(AF_INET, SOCK_STREAM, 0);
  
  //Setting up the sockaddr svraddr structure
  SvrAddr.sin_family = AF_INET;
  SvrAddr.sin_addr.s_addr = inet_addr(IPAddress);
  SvrAddr.sin_port = htons(PortNumber);
  
  
  //Bind
  bind(WelcomeSocket, (sockaddr*)&amp;SvrAddr, sizeof(SvrAddr));
  
  //listening
  //listen(WelcomeSocket, 5);
  
  //temp users
  Users temp;
  //ConnectionSocket = accept(WelcomeSocket, NULL, NULL);
  ConnectionSocket = SOCKET_ERROR;
  
  //while loop for the connection 
  while (1) {
  
      listen(WelcomeSocket, 5);
  
      if ((ConnectionSocket = accept(WelcomeSocket, NULL, NULL)) == SOCKET_ERROR)
      {
          return -1;
      }
  
      else {
  
      int n = recv(ConnectionSocket, RxBuffer, sizeof(RxBuffer), 0);
  
      if (n == 0)
          break;
  
  
      memcpy(&amp;temp, RxBuffer, sizeof(struct Users));
  
      //cout &lt;&lt; temp.message &lt;&lt; temp.userName &lt;&lt; endl &lt;&lt; endl;
  
      //check which message type is being sent
      switch(temp.message) {
  
      //if message type 1
      case 1 :
          for (int i = 0; i &lt; 2; i++) {
  
              //if receieved username matches with any username in the database
              if (strcmp(temp.userName, client[i].userName) == 0) {
  
                  //assign the recieved users information to the matched one in database
                  strcpy(client[i].userName, temp.userName);
                  client[i].online = true;
                  client[i].message = 2;
  
                  cout &lt;&lt; "Username: " &lt;&lt; client[i].userName &lt;&lt; endl &lt;&lt; "Online status: " &lt;&lt; client[i].online &lt;&lt; endl &lt;&lt; endl;
  
                  //send the acknowledgement packet
                  send(ConnectionSocket, (char *)&amp;client[i], sizeof(struct Users), 0);
                  //closesocket(ConnectionSocket);
              }
  
          }
          break;
  
      //if message type 3
      case 3 :
  
          cout &lt;&lt; "User being searched for: " &lt;&lt; temp.userName &lt;&lt; endl &lt;&lt; endl;
          for (int i = 0; i &lt; 2; i++) {
  
              //if receieved username matches with any username in the database
              if (strcmp(temp.userName, client[i].userName) == 0) {
                  client[i].message = 4;
                  //send the acknowledgement packet
                  send(ConnectionSocket, (char *)&amp;client[i], sizeof(struct Users), 0);
                  //closesocket(ConnectionSocket);
              }
  
          }
          break;
  
      default :
          break;
  
      }
  
  }
  
  }
  
  closesocket(ConnectionSocket);
  WSACleanup();
  }
  </code></pre>
  
  <p>Client (same for both)</p>
  
  <pre><code>struct Users {
  
  int message;
  char userName[50];
  char ipAddress[50];
  int PortNumber;
  bool online;
  
  };
  
  
  int main () {
  
  struct sockaddr_in SvrAddr;
  SOCKET ClientSocket;
  int PortNumber = 20000;
  char IPAddress[] = "127.0.0.1";
  //char message[] = "Hello this is the client.";
  char RxBuffer[128];
  
  WORD wVersionRequested; 
  WSADATA wsaData; 
  
  wVersionRequested = MAKEWORD(2,3); //For Client 
  
  if(WSAStartup(wVersionRequested, &amp;wsaData) != 0) 
      return -1; 
  
  
  ClientSocket = socket(AF_INET, SOCK_STREAM, 0);
  
  SvrAddr.sin_family = AF_INET;
  SvrAddr.sin_addr.s_addr = inet_addr(IPAddress);
  SvrAddr.sin_port = htons(PortNumber);
  
  connect(ClientSocket, (sockaddr*)&amp;SvrAddr, sizeof(SvrAddr));
  
  
  
  //cout &lt;&lt; "Name: ";
  //cin &gt;&gt; login;
  
  //Send request to login
  int log;
  char * name = new char[128];
  char * request = new char[128];
  Users client;
  Users talkto;
  
  
  do {
  
      cout &lt;&lt; "To login press (1) to end press (2). ";
      cin &gt;&gt; log;
      flushall();
  
      if (log == 1) {
  
          cout &lt;&lt; "Username : ";
          cin.getline(name, 128, '\n');
          cout &lt;&lt; endl;
          flushall();
  
          //Set client login info
          strcpy(client.userName, name);
          client.message = 1;
  
  
  
          send(ClientSocket, (char *)&amp;client, sizeof(struct Users), 0);
  
  
          //Recieve acknowledgement
          recv(ClientSocket, RxBuffer, sizeof(RxBuffer), 0);
          //create temp users
          Users temp;
  
          memcpy(&amp;temp, RxBuffer, sizeof(struct Users));
  
          //If logged in and received a message of type 2 (acknowledgement)
          if (temp.message == 2) {
  
              cout &lt;&lt; "You have logged in." &lt;&lt; endl &lt;&lt; endl;
  
              cout &lt;&lt; "Enter user to request user information: ";
              cin.getline(talkto.userName, 128, '\n');
              flushall();
              cout &lt;&lt; endl;
  
              talkto.message = 3;
  
              //send request for user information packet
              send(ClientSocket, (char *)&amp;talkto, sizeof(struct Users), 0);
  
              recv(ClientSocket, RxBuffer, sizeof(RxBuffer), 0);
  
              memcpy(&amp;temp, RxBuffer, sizeof(struct Users));
  
              //if message received is of type 4 (acknowledgement of user request info)
              if (temp.message == 4) {
  
                  cout &lt;&lt; "Requested User: " &lt;&lt; temp.userName &lt;&lt; endl &lt;&lt; "Online status: " &lt;&lt; temp.online &lt;&lt; endl &lt;&lt; endl;
              }
          }
  
          //cout &lt;&lt; temp.userName &lt;&lt; endl &lt;&lt; temp.online &lt;&lt; endl &lt;&lt; temp.message;
      } 
  
  } while (log != 2);
  
  
  closesocket(ClientSocket);
  
  
  WSACleanup();
  
  
  
  } 
  </code></pre>