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

  • SEARCH
  • Home
  • 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 7782183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:27:40+00:00 2026-06-01T19:27:40+00:00

At the moment I am creating a client server console application in c++. With

  • 0

At the moment I am creating a client server console application in c++. With the use of the winsock2.h library and UDP protocol we use the sendto and recvfrom to send messages as strings from the client to the server which will then send the message to a different client, right now if the client1 sends a message to client2, client2 will not receive the message until they attempt to send a message to client1. I am looking to make the program work like an instant messenger, so that when client1 sends a message to client2, client2 will receive it almost instantly without having to send a message first.

Also if client1 were to send a message to client2, client1 would not be able to send another message unless client2 had replied to the first one.

If you need more information or to see some code just ask.

The send code:

while( getline( cin, line ) )
{
    // send a string to the server.
    sendto( hSocket, line.c_str(), line.size(), 0, 
        reinterpret_cast<sockaddr*>( &serverAddress ),
        sizeof( serverAddress ) );

    // recieve the response.
    int n = recvfrom( hSocket, recvLine, MAXLINE, 0, NULL, NULL );
    if( n == -1 )
    {
        cout << "no reply" << endl;
    }
    else
    {
        recvLine[n] = 0;
        string const terminateMsg = "server exit";
        string msg = recvLine;
        if( msg == terminateMsg )
        {
            cout << "Server terminated" << endl;
            break;
        }
        cout << n << ": " << recvLine << endl;
    }
}
  • 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-06-01T19:27:41+00:00Added an answer on June 1, 2026 at 7:27 pm

    right now if the client1 sends a message to client2, client2 will not
    receive the message until they attempt to send a message to client1.

    you’ve coded it that way.

    Also if client1 were to send a message to client2, client1 would not
    be able to send another message unless client2 had replied to the
    first one.

    Again, you’ve coded it that way. Your code assumes every sendto() is followed by a recvfrom(). But your question indicates that isn’t what you want. I added some comments to your code below

    while( getline( cin, line ) )
    {
        // send a string to the server. <-- will never get here a 2nd time through the loop without recvfrom() returning
        sendto( hSocket, line.c_str(), line.size(), 0, 
            reinterpret_cast<sockaddr*>( &serverAddress ),
            sizeof( serverAddress ) );
    
        // recieve the response. <--- will never get here without getline() returning
        int n = recvfrom( hSocket, recvLine, MAXLINE, 0, NULL, NULL );
        if( n == -1 )
        {
            cout << "no reply" << endl;
        }
        else
        {
            recvLine[n] = 0;
            string const terminateMsg = "server exit";
            string msg = recvLine;
            if( msg == terminateMsg )
            {
                cout << "Server terminated" << endl;
                break;
            }
            cout << n << ": " << recvLine << endl;
        }
    }
    

    I am looking to make the program work like an instant messenger, so
    that when client1 sends a message to client2, client2 will receive it
    almost instantly without having to send a message first.

    Use Boost.Asio and asynchronous sockets, the async chat_client example does this very thing perfectly using TCP sockets, it is fairly trivial to modify the example for UDP. The async UDP echo server example may be useful as well. You’ll want to use boost::asio::ip::udp::socket::async_recv_from() and boost::asio::ip::udp::socket::async_send_to(). The other BSD socket interfaces have equivalent mappings described in the documentation.

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

Sidebar

Related Questions

I'm creating a library for handling client caching over HTTP on a web server
I can't use flock at the moment(server restrictions) so I am creating a alternative
I'm creating an installer for a client at the moment but I know that
I'm creating a facebook application at the moment and need to create a comment
At the moment I am working on creating a small library for common data
I am creating an application where client-side functionality calls various services to return results/content
I am creating a file server application for school assignment. What I currently have
I am creating an application to upload data to a server. The data will
I am creating a java Sudoku GUI application at the moment. The grid for
Assume for a moment that you are creating a commercial product for SharePoint. This

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.