Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3482082
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:31:05+00:00 2026-05-18T10:31:05+00:00

I have a simple (very simple:) ) client and server which can send text

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:31:06+00:00Added an answer on May 18, 2026 at 10:31 am

    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. select can 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 of WaitForMultipleObjects, GetStdHandle and WSAEventSelect. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple TCP server written in C. It runs indefinitely, waiting
I have a very simple problem which requires a very quick and simple solution
I have a very simple WPF application in which I am using data binding
I have a very simple Java RMI Server that looks like the following: import
I have very simple query. I want to make sure that I don't have
Very simple I have this: for i in self.Abilities: i.OnTimer(amount) Can i have something
I have a very simple ruby rack app which retrieves information from a webservice
i have very simple problem. I need to create model, that represent element of
I have very simple select like this: SELECT * FROM table WHERE column1 IN
I have a very simple bit of script that changes the status of an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.