I have a simple (very simple:) ) client and server which can send text messages over TCP, It’s flawed because I don’t know how to listen to the messages and print them on the screen as well as send messages at the same time (for both client and server).
Also I have difficulty in presenting all the sent and received messages between the client and server in the order the messages were sent.
For example on the server side the chat may look like this
Server:Hi there
Client:Hi
And on the client side the same chat looks like this
Client:Hi
Server:Hi there
The messages are in different order and both users see differently. My main question is, how would I go about synchronizing the output so that they both see the same thing?
Keeping in mind what I mentioned earlier about not knowing how to listen and send simultaneously, this is the message loop for the sever and client.
Server message loop
while(true){
cout<<"-[SERVER]: ";
getline(cin,send_text);
if (sizeof(send_text) > 0 ){
bytes_out = send(client,send_text.c_str(),send_text.length()+1,0);
cout<< endl;
if (bytes_out == SOCKET_ERROR){
cout<<"-[SERVER error in sending.]" << endl;
break;
}
}
bytes_in = recv(client,recvd_text,sizeof(recvd_text),0);
if (bytes_in > 0 ){
cout<<"-[CLIENT]: " << recvd_text << endl; //output on screen
}
if (bytes_in == 0){
cout<<"-[CLIENT has disconnected.]" << endl;
break;
}
if (bytes_in == SOCKET_ERROR){
cout<<"-[CLIENT closed unexpectedly.]" << endl;
break;
}
}
Client message loop
while (true){
cout<<"-[CLIENT]: ";
getline(cin,send_text);
if(sizeof(send_text) > 0){
bytes_out = send(con_sock,send_text.c_str(),send_text.length()+1,0);
if (bytes_out == SOCKET_ERROR){
cout<<"-[CLIENT error in sending.]" << endl;
break;
}
}
bytes_in = recv(con_sock,recvd_text,sizeof(recvd_text),0);
if (bytes_in > 0){
cout<<"-[SERVER]: " << recvd_text << endl;
}
if (bytes_in == 0){
cout<<"-[Server has disconnected." << endl;
break;
}
if (bytes_in == SOCKET_ERROR){
cout<<"-[Server closed unexpectedly." << endl;
break;
}
}
return true;
The core problem is how to wait for incoming messages and for user input at the same time. (Once you’ll be able to do it, the “synchronization” will follow naturally, since messages will be displayed as they are sent or received).
The solution to this problem is using
select.selectcan wait for input on several file handles (e.g. standard input and a socket) and return when there’s data available. You can then handle the data: if it’s from the socket, display it. If it’s from the user, send it to the remote host.Here’s an example chat client/server program using
select. (The example is in Python not C++, but the principle is the same).Windows-specific
In Windows,
select()is provided by the Winsock library and only works on sockets. To wait for input from the console and from a network socket, it looks like you will need to use a combination ofWaitForMultipleObjects,GetStdHandleandWSAEventSelect. In Unix-like environments it’s much simpler because the standard input and sockets are all file descriptors.A simpler solution in Windows would be a message-passing based one. Use a window for the text input, and use WSAAsyncSelect to get a message when network input is ready.