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 8786613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:40:47+00:00 2026-06-13T21:40:47+00:00

I got all of this code from the beej guide so all of the

  • 0

I got all of this code from the beej guide so all of the accepting can be seen from there. In the Beej’s code, he gets a message from a client, and then sends the message to all the other clients. This can be seen from this snippet here:

                // handle data from a client
                if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
                    // got error or connection closed by client
                    if (nbytes == 0) {
                    //handle error
                    }
                } 
                else {
                    // we got some data from a client
                    for(j = 0; j <= fdmax; j++) {
                        // send to everyone!
                        if (FD_ISSET(j, &master)) {
                            // except the listener and ourselves
                            if (j != listener && j != i) {
                                if (send(j, buf, nbytes, 0) == -1) {
                                    perror("send");
                                }
                            }
                        }
                    }
                }
            } // END handle data from client

Instead of sending the same message to all the clients, i would like to adapt it into a request/reply feature and send a reply to the same client I received data from.

here is what I have so far:

long length = 0;
string stringRead;
messagebroker broker;
read( i, &length, sizeof( length ) );
length = ntohl( length );
if(length > -1)
while ( 0 < length ) {
     char buffer[1024];
     int cread;
     cread = read( i, buffer, min( sizeof( buffer ), length ) );
     stringRead.append( buffer, cread );
     length -= cread;
 }
cout << "Got Message: " + stringRead << endl;
string response = broker.handleMessage(stringRead.c_str());

cout << "sending response" << response << endl;

//socket ready for writing
if (FD_ISSET(i, &master)) { //should i check to see if write_fds? I have this here
                                //simply because the guide has it, but i am suspicious
                                //it is there so we can not write to the master.
  length = htonl( response.length() );
  cout << "sent length" << endl;
  if(send( i, &length, sizeof(length) , 0) == 0){
     fprintf(stderr, "Error sending data %d\n", errno);
     exit(3);
  }
  if(send( i, response.data(), response.length(),0 )== 0){
          fprintf(stderr, "Error sending data %d\n", errno);
          exit(3);
      }
    }   //end if

I receive all data from the client at the server. I then am not sure if the problem is writing the data back on the server, or reading from the client. I assume it is writing to the client from the server. As I hinted in the comments, I think I know where I went wrong, but I have removed this if statement, and I still wasn’t able to read anything on the client side. Do I need to set a writable flag at the very beginning? Please let me know if you need anything else. Sorry this post was so long.

  • 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-13T21:40:49+00:00Added an answer on June 13, 2026 at 9:40 pm

    Just do the write. If it returns -1/EWOULDBLOCK, or a value indicating that it didn’t write the full response, then you add the FD to the writefds, and continue the write when the FD becomes writable. You normally don’t have any writefds, as FDs are normally writable, that is to say they normally have space in their socket send buffers.

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

Sidebar

Related Questions

Hallo all. I got this piece of code: <div> <input id=id1 name=radioButton type=radio> <input
I got the javascript email validation code from this post . According to the
Got this code from here http://www.irunmywebsite.com/raphael/drawtool2.php . Runs great in IE when I tested
I got this code from somewhere here to show only author's comment in dashboard.
So I've got all of this really neato PHP code and I've started doing
Ive got this code from an ebook tutorial on embedding MPMoviePlayerController from a VIEW
I've got this code from an example I found useful around Internet. It uses
I've currently got all of this mess at the top of my ViewModels which
Morning y'all This is probably an easy one but I barely got any sleep
I got this url /search/renttype-all.place-all.type-all.bedrooms-all.0.0/ I want to get the text after the second

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.