title: c++ server client chat
body: <p>I'm making a server, client app in c++ console based.</p>
  
  <p>What I did so far: </p>
  
  <ul>
  <li>I can connect to the server.</li>
  <li>I can send messages to the server.</li>
  <li>The server can send the messages back.</li>
  </ul>
  
  <p>But what I can't figure out, how can I let the server act also as a client to send messages to the client while he is processing received messages from the client?</p>
  
  <p>People can use it as an example as well :D</p>
  
  <p>Well I will post also some parts of the code:</p>
  
  <p>server:</p>
  
  <pre><code>  #include "stdafx.h"
    using namespace std;
   //our main function
   void main()
       {
  int numClients;
  long antwoord;
  char chatname[100];
  char bericht[250]; //messages
  char sbericht[250]; //smessages
       //here we set the Winsock-DLL to start
  
  WSAData wsaData;
  WORD DLLVERSION;
  DLLVERSION = MAKEWORD(2,1);
  
  //here the Winsock-DLL will be started with WSAStartup
                  //version of the DLL
  antwoord = WSAStartup(DLLVERSION, &amp;wsaData);
  
  if(antwoord != 0)
  {
      WSACleanup();
      exit(1);
  }
  else
  {
      cout &lt;&lt; "WSA started successfully" &lt;&lt;endl;
      cout &lt;&lt; "The status: \n" &lt;&lt; wsaData.szSystemStatus &lt;&lt;endl;
  }
  //the DLL is started
  
  //structure of our socket is being created
  SOCKADDR_IN addr; 
  
  //addr is our struct
  
  int addrlen = sizeof(addr);
  
  //socket sListen - will listen to incoming connections
  SOCKET sListen;
  //socket sConnect - will be operating if a connection is found.
  SOCKET sConnect;
  
  //setup of our sockets
                  //opgezocht op internet - AF_INET bekend dat het lid is van de internet familie
                              //Sock_STREAM  betekenend dat onze socket een verbinding georiënteerde socket is.
  sConnect = socket(AF_INET,SOCK_STREAM,NULL);
  
  //now we have setup our struct
  
  //inet_addr is our IP adres of our socket(it will be the localhost ip
  //that will be 127.0.0.1
  
  addr.sin_addr.s_addr = inet_addr("192.168.1.103");
  
  //retype of the family
  addr.sin_family = AF_INET;
  
  //now the server has the ip(127.0.0.1) 
  //and the port number (4444)
  addr.sin_port = htons(4444);
  
  //here we will define the setup for the sListen-socket
  sListen = socket(AF_INET,SOCK_STREAM,NULL);
  
  if (sConnect == INVALID_SOCKET)
  {
      cout &lt;&lt; "Error at socket(): \n" &lt;&lt; WSAGetLastError() &lt;&lt;endl;
      WSACleanup();
  }
  else
  {
      cout &lt;&lt; "Connect socket() is OK!" &lt;&lt;endl;
  }
  
  if(sListen == INVALID_SOCKET)
  {
      cout &lt;&lt; "Error at socket(): \n" &lt;&lt; WSAGetLastError() &lt;&lt;endl;
      WSACleanup();
  }
  else
  {
      cout &lt;&lt; "Listen socket() is OK!" &lt;&lt;endl;
  }
  //here the sListen-socket will be bind
  //we say that the socket has the IP adress of (127.0.0.1) and is on port (4444)
  //we let the socket become the struct "addr"
  if(bind(sListen, (SOCKADDR*)&amp;addr, sizeof(addr)) == SOCKET_ERROR)
  {
      cout &lt;&lt; "bind() failed: \n" &lt;&lt; WSAGetLastError() &lt;&lt;endl;
      WSACleanup();
      exit(1);
  }
  else{
      cout &lt;&lt; "bind() is OK!" &lt;&lt;endl;
  }
  
  
  //here we will tell what the server must do when a connection is found
  //therefor we will create an endless loop
  cout &lt;&lt; "Waiting for a incoming connection..." &lt;&lt;endl;
  for(;;)
  {
  
          //now we let the socket listen for incoming connections
              //SOMAXCOMM heeft het nut dat het dan voordurend luisterd naar inkomende verbindingen zonder limiet
          listen(sListen, SOMAXCONN);
          while(numClients &lt; SOMAXCONN)
          {
              //if a connection is found: show the message!
              if(sConnect = accept(sListen, (SOCKADDR*)&amp;addr, &amp;addrlen))
              {
                  cout &lt;&lt; "A Connection was found!" &lt;&lt;endl;
  
                  antwoord = send(sConnect, "Welcome to our chat:", 21,NULL);
  
                  if(antwoord &gt; 1)
                  {
  
                      antwoord = recv(sConnect, sbericht, sizeof(sbericht), NULL);
                      antwoord = recv(sConnect, chatname, sizeof(chatname), NULL);
  
                          while(antwoord = recv(sConnect, sbericht, sizeof(sbericht), NULL) &amp;&amp; (antwoord = recv(sConnect, sbericht, sizeof(sbericht), NULL)) )
                          {
                              antwoord = send(sConnect, sbericht, sizeof(sbericht), NULL);
                              antwoord = send(sConnect, chatname, sizeof(chatname), NULL);    
                          }
  
                  }
                  else
                  {
                  cout &lt;&lt; "The connection to the client has been lost... \n" &lt;&lt; "please exit the server." &lt;&lt;endl;
                  break;
                  }
                  numClients++;
              }
          }
  
  
  }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>    // ChatServer.cpp : Defines the entry point for the console application.
      //
      //include of the stdafx.h file where importent files are being included
  
      #include "stdafx.h"
  
      using namespace std;
  
      void smessage()
      {
  
      }
     //our main function
     int main()
     {
  //here we set the Winsock-DLL to start
  string bevestiging; 
  
  char chatname[100]; 
  
  char bericht[250];
  char sbericht[250];
  
  string strbericht;
  
  string strsbericht;
  
  long antwoord;
  //here the Winsock-DLL will be started with WSAStartup
                  //version of the DLL
  WSAData wsaData;
  WORD DLLVERSION;
  DLLVERSION = MAKEWORD(2,1);
  antwoord = WSAStartup(DLLVERSION, &amp;wsaData);
  if(antwoord != 0)
  {
      exit(1);
  }
  else
  {
      cout &lt;&lt; "WSA started successfully" &lt;&lt;endl;
      cout &lt;&lt; "The status: \n" &lt;&lt; wsaData.szSystemStatus &lt;&lt;endl;
  }
  
  SOCKADDR_IN addr;
  
  int addrlen = sizeof(addr);
  
  SOCKET sConnect;
  
  sConnect = socket(AF_INET, SOCK_STREAM, NULL);
  
  if (sConnect == INVALID_SOCKET)
  {
      cout &lt;&lt; "Error at socket(): \n" &lt;&lt; WSAGetLastError() &lt;&lt;endl;
  }
  else
  {
      cout &lt;&lt; "socket() is OK!\n" &lt;&lt;endl;
  }
  
  
  
  addr.sin_addr.s_addr = inet_addr("192.168.1.103");
  
  addr.sin_family = AF_INET;
  
  addr.sin_port = htons(4444);
  
  cout &lt;&lt; "What is your chat name?" &lt;&lt;endl;
  
  cin.getline(chatname, 100);
  
  
  cout &lt;&lt; "Do you want to connect to the server? [Y/N]" &lt;&lt;endl;
  
  cin &gt;&gt; bevestiging;
  
  
  if (bevestiging == "N")
  {
      exit(1);
  }
  else
  {
      if(bevestiging == "Y")
      {
  
          connect(sConnect, (SOCKADDR*)&amp;addr, sizeof(addr));
  
          antwoord = recv(sConnect, bericht, sizeof(bericht), NULL);
  
          strbericht = bericht;
  
          cout &lt;&lt; strbericht &lt;&lt; chatname &lt;&lt;endl;
  
          while(true)
          {
              if(antwoord &gt; 1)
              {
  
                  cin.clear();
                  cin.sync();
                  cout &lt;&lt; chatname &lt;&lt; " :" &lt;&lt;endl;
                  cin.getline(sbericht, 250);
                  antwoord = send(sConnect, sbericht, sizeof(sbericht), NULL);
                  antwoord = send(sConnect, chatname, sizeof(chatname), NULL);
  
                  while(antwoord = send(sConnect, sbericht, sizeof(sbericht), NULL) &amp;&amp; (antwoord = send(sConnect, sbericht, sizeof(sbericht), NULL)))
                  {
                      antwoord = recv(sConnect, sbericht, sizeof(sbericht), NULL);
                      antwoord = recv(sConnect, chatname, sizeof(chatname), NULL);
                      cout &lt;&lt; chatname &lt;&lt; ":" &lt;&lt;endl;
                      cout &lt;&lt; sbericht &lt;&lt;endl;
                      cin.getline(sbericht, 250);
  
                  }
  
              }
  
              else
              {
              cout &lt;&lt; "The connection to the server has been lost... \n" &lt;&lt; "please exit the client." &lt;&lt;endl;
  
              }
          }
      }
  }
      }
  </code></pre>