title: C++ Winsock communication between Server->Client and vice versa
body: <p>I'm trying to make a simple chat program between to programs on a lan. My problem is I can easily connect and send message from the server to client but not the other way around. I'm trying to make it work like this. Server&lt;->Client as in constantly sending and receiving between both programs, obviously that would require a loop for continuous input but I'm not quite sure how to do it.  Here is the code for both server and client. </p>
  
  <pre><code>//THIS IS THE SERVER 
      int main(int argc, char *argv[])
  {
    WSADATA wsaData;
    int starterr = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
    if (starterr != 0) 
    {
        cout &lt;&lt; "WSADATA Failed to startup!" &lt;&lt; endl;
        cout &lt;&lt; "Error Code: " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
        system("pause &gt;nul");
        WSACleanup();
        return 0;
    }
  
    cout &lt;&lt; "WSADATA Startup Successful!" &lt;&lt; endl;
    SOCKET mysock = socket(AF_INET, SOCK_STREAM, 0);
    if (mysock == INVALID_SOCKET)
    {
        cout &lt;&lt; "Socket Creation Failed!" &lt;&lt; endl;
        cout &lt;&lt; "Error Code: " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
        system("pause &gt;nul");
        WSACleanup();
        return 0;
    }
    cout &lt;&lt; "Socket Creation Successful!" &lt;&lt; endl;
    sockaddr_in sin;
    sin.sin_port = htons(80);
    sin.sin_addr.s_addr = INADDR_ANY;
    sin.sin_family = AF_INET;
    if (bind(mysock,(sockaddr*)&amp;sin, sizeof(sin)) == SOCKET_ERROR)
    {
        cout &lt;&lt; "Socket failed to bind!" &lt;&lt; endl;
        cout &lt;&lt; "Error Code: " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
        system("pause &gt;nul");
        WSACleanup();
        return 0;
    }
    cout &lt;&lt; "Socket Binded Successfuly!" &lt;&lt; endl;
  
    //Listen to the socket until successful on receiving input
    while (listen(mysock, SOMAXCONN) == SOCKET_ERROR);
    SOCKET client;
  
    int lin = sizeof(sin);
    client = accept(mysock,(sockaddr*) &amp;sin, &amp;lin);
    cout &lt;&lt; "Connection Established!" &lt;&lt; endl;
    char buf[200] = "Message from server to client\n";
  
  
    //Send an initial message to the client 
  
    send(client, buf, sizeof(buf), 0);
  
    //but then how do I wait for a message from the client again here??
    //ive tried recv here before with no luck :/
  
  
  
  
  
  
  
    closesocket(mysock);
    closesocket(client);
    WSACleanup();
    system("pause &gt;nul");    
    return 0;
  }
  
  
  
  
  
     //Client program here
      int main(int argc, char *argv[])
      {
        WSADATA wsaData;
        int starterr = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
        if (starterr != 0) 
        {
            cout &lt;&lt; "WSADATA startup has failed!" &lt;&lt; endl;
            cout &lt;&lt; "Error Code: " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
            system("pause &gt;nul");
            WSACleanup();
            return 0;
        }
        cout &lt;&lt; "WSADATA Startup Successful!" &lt;&lt; endl;
        SOCKET mysock = socket(AF_INET, SOCK_STREAM, 0);
  
        if (mysock == INVALID_SOCKET)
        {
            cout &lt;&lt; "Socket Creation Failed!" &lt;&lt; endl;
            cout &lt;&lt; "Error Code:  " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
            system("pause &gt;nul");
            WSACleanup();
            return 0;
        }
  
        cout &lt;&lt; "Socket Creation Successful!" &lt;&lt; endl;
        sockaddr_in sin;
        sin.sin_port = htons(80);
        sin.sin_addr.s_addr = inet_addr("127.0.0.1");//ip for connection
        sin.sin_family = AF_INET;
        if (connect(mysock,(sockaddr*)&amp;sin, sizeof(sin)) == INVALID_SOCKET) 
        {
            cout &lt;&lt; "Socket Connection Failed" &lt;&lt; endl;
            cout &lt;&lt; "Error Code:  " &lt;&lt; WSAGetLastError() &lt;&lt; endl;
            system("pause &gt;nul");
            closesocket(mysock);
            WSACleanup();
            return 0;
        }
        cout &lt;&lt; "Socket Has Connected Successfuly!" &lt;&lt; endl;
  
  
        //Same applies here, receive message but exactly how do I send one back 
  
        char buf[200];
        recv(mysock, buf, sizeof(buf), 0);
        cout &lt;&lt; buf;
  
        //send here seemed to bring up a bunch of symbols
  
        system("pause &gt;nul");    
        WSACleanup();
  
        closesocket(mysock);
  
        return 0;
      }
  </code></pre>