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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:43:45+00:00 2026-05-22T15:43:45+00:00

I need to send a set of bit fields along with a string of

  • 0

I need to send a set of bit fields along with a string of characters from a client to a server.

So given I have:

#define YES 1
#define FLAG 2
int main(int argc, char* argv[])
{
    return sendToServer("The Message", YES | FLAG);
}
int sendToServer(char* msg, int bitfields)
{
   /* create socket and connect to server */
   /* Assume sock is set */
   send(sock, msg, strlen(msg), 0);
   return 0;
}

What would be the best way to send the bitfields? Is there anyway to send the bitfields along with the string?

EDIT: Ok I’m trying to implement Vlad’s method. My client is pretty much identical to what he wrote. I have put the flag at the beginning data[0] and I used htonl instead of bswap. My server:

int main(int argc, char* argv[])
{
    /* create socket and wait for connection */
    char buffer[BUFFERSIZE];
    size_t rcvdB = recv(clntSock, buffer, sizeof(int),0);
    int flags = ntohl((int) buffer);
    rcvdB = recv(clntSock, buffer, sizeof(size_t),0);
    size_t msgSize = ntohl((size_t) buffer);
    rcvdB = recv(clntSock,buffer,msgSize,0);
    /* Then I send back to the client */
    ssize_t sntB = send(clntSock,buffer,msgSize,0);
}

When the client prints the message there are multiple ascii characters at the end of the message.

EDIT2:

The issue seems to occur when I read more than 8 bytes of data

  • 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-22T15:43:46+00:00Added an answer on May 22, 2026 at 3:43 pm

    As others have pointed out, it depends on a protocol. I’d use something like this:

    int sendToServer(char* msg, int bitfields)
    {
       unsigned int bits = bswap32 (bitfields); // Convert host to network byte order.
    
       size_t len = strlen (msg);   // Calculate string length.
       size_t nlen = bswap64 (len); // Convert length's byte order from host to network.
    
       iovec data[3]; // Prepare 3 I/O buffers to send data by performing a single system call (they are expensive).
    
       // Send length of the string first.
       data[0].iov_base = &nlen;
       data[0].iov_len = sizeof (nlen);
    
       // Send string...
       data[1].iov_base = msg;
       data[1].iov_len = len;
    
       // And, of course, send bits.
       data[2].iov_base = &bits;
       data[2].iov_len = sizeof (bits);
    
       // Write all of those to the socket.
       writev (fd, &data, sizeof (data) / sizeof (data[0]));
    }
    

    On the receiving side, you can read the first sizeof (size_t) bytes, convert from network to host byte order, cast to size_t. That will tell you length of string. Then read buffer of that length – that will be your string. Finally, read another sizeof (int) bytes – that will be your bitfield.

    See also:

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

Sidebar

Related Questions

I need to send a very specific (non-standard) string to an FTP server: dir
I need to send a CSV file in HTTP response. How can I set
I need to send MMS thought a C# application. I have already found 2
I need to send PUT and DELETE along with POST, GET to a REST
I need to send emails to a list of IDs from the contact application,
We need to send uploaded files from our CMS website to any antivirus product
I need to send hundreds of newsletters, but would like to check first if
I need to send and receive data over serial connections (RS-232 and RS-422). How
We need to send email which contains Pound (currency) symbols in ColdFusion. Before sending
I'm failing at finding the commands I need to send to authenticate to a

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.