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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:44:59+00:00 2026-05-26T03:44:59+00:00

I’m working with the NetLink socket library ( https://sourceforge.net/apps/wordpress/netlinksockets/ ), and I want to

  • 0

I’m working with the NetLink socket library ( https://sourceforge.net/apps/wordpress/netlinksockets/ ), and I want to send some binary data over the network in a format that I specify.

The format I have planned is pretty simple and is as follows:

  • Bytes 0 and 1: an opcode of the type uint16_t (i.e., an unsigned integer always 2 bytes long)

  • Bytes 2 onward: any other data necessary, such as a string, an integer, a combination of each, etc.. the other party will interpret this data according to the opcode. For example, if the opcode is 0 which represents “log in”, this data will consist of one byte integer telling you how long the username is, followed by a string containing the username, followed by a string containing the password. For opcode 1, “send a chat message”, the entire data here could be just a string for the chat message.

Here’s what the library gives me to work with for sending data, though:

void send(const string& data);
void send(const char* data);
void rawSend(const vector<unsigned char>* data);

I’m assuming I want to use rawSend() for this.. but rawSend() takes unsigned chars, not a void* pointer to memory? Isn’t there going to be some loss of data here if I try to cast certain types of data to an array of unsigned chars? Please correct me if I’m wrong.. but if I’m right, does this mean I should be looking at another library that has support for real binary data transfer?

Assuming this library does serve my purposes, how exactly would I cast and concatenate my various data types into one std::vector? What I’ve tried is something like this:

#define OPCODE_LOGINREQUEST 0

std::vector<unsigned char>* loginRequestData = new std::vector<unsigned char>();
uint16_t opcode = OPCODE_LOGINREQUEST;
loginRequestData->push_back(opcode);
// and at this point (not shown), I would push_back() the individual characters of the strings of the username and password.. after one byte worth of integer telling you how many characters long the username is (so you know when the username stops and the password begins)
socket->rawSend(loginRequestData);

Ran into some exceptions, though, on the other end when I tried to interpret the data. Am I approaching the casting all wrong? Am I going to lose data by casting to unsigned chars?

Thanks in advance.

  • 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-26T03:45:00+00:00Added an answer on May 26, 2026 at 3:45 am

    I like how they make you create a vector (which must use the heap and thus execute in unpredictable time) instead of just falling back to the C standard (const void* buffer, size_t len) tuple, which is compatible with everything and can’t be beat for performance. Oh, well.

    You could try this:

    void send_message(uint16_t opcode, const void* rawData, size_t rawDataSize)
    {
        vector<unsigned char> buffer;
        buffer.reserve(sizeof(uint16_t) + rawDataSize);
    #if BIG_ENDIAN_OPCODE
        buffer.push_back(opcode >> 8);
        buffer.push_back(opcode & 0xFF);
    #elseif LITTLE_ENDIAN_OPCODE
        buffer.push_back(opcode & 0xFF);
        buffer.push_back(opcode >> 8);
    #else
        // Native order opcode
        buffer.insert(buffer.end(), reinterpret_cast<const unsigned char*>(&opcode), 
            reinterpret_cast<const unsigned char*>(&opcode) + sizeof(uint16_t));
    #endif
        const unsigned char* base(reinterpret_cast<const unsigned char*>(rawData));
        buffer.insert(buffer.end(), base, base + rawDataSize);
        socket->rawSend(&buffer); // Why isn't this API using a reference?!
    }
    

    This uses insert which should optimize better than a hand-written loop with push_back(). It also won’t leak the buffer if rawSend tosses an exception.

    NOTE: Byte order must match for the platforms on both ends of this connection. If it does not, you’ll need to either pick one byte order and stick with it (Internet standards usually do this, and you use the htonl and htons functions) or you need to detect byte order (“native” or “backwards” from the receiver’s POV) and fix it if “backwards”.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.