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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:26:47+00:00 2026-05-29T13:26:47+00:00

Consider I have Struct like the following: struct Bitmask { unsigned char payload_length: 7;

  • 0

Consider I have Struct like the following:

struct Bitmask
{
  unsigned char payload_length: 7;
  unsigned char mask: 1;
  unsigned char opcode: 4;
  unsigned char rsv3: 1;
  unsigned char rsv2: 1;
  unsigned char rsv1: 1;
  unsigned char fin: 1;
};

const char* payload = "Hello";
const size_t payload_length = strlen(payload);

Bitmask* header = new Bitmask();
header->fin =1;
header->rsv1 = 0;
header->rsv2 = 0;
header->rsv3 = 0;
header->opcode = 1;
header->mask = 0;
header->payload_length = payload_length;

iovec iov[2];
iov[0].iov_base = (char*)header;
iov[0].iov_len = sizeof (header);
iov[1].iov_base = (char *)payload;
iov[1].iov_len = strlen(payload);

ACE_DEBUG ((LM_DEBUG,
            ACE_TEXT ("iov[0].length = %d\niov[1].length = %d\n"),
            iov[0].iov_len,
            iov[1].iov_len));

size_t bytes_xfered;
client_stream_.sendv_n (iov, 2, 0, &bytes_xfered);

cout << "Transfered " << bytes_xfered << " byte(s)" << std::endl;

I am initializing it with appropriate values. Finally, I want to convert the struct into char* so I can append my payload (which is char* message) and send it over a websocket connection.

  • 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-29T13:26:48+00:00Added an answer on May 29, 2026 at 1:26 pm

    Is a struct’s address the same as its first member’s address?

    Yes, this is actually mandated by the C and C++ standards. From the C standard:

    6.7.2.1-13. A pointer to a structure object, suitably converted, points to its initial member

    The size of your struct should be two bytes. You should not convert a pointer to it to char*, though: instead, you should use memcpy to copy your Bitmask into the buffer that you send over the network.

    EDIT Since you use scatter-gather I/O with iovec, you do not need to cast Bitmask to anything: iov_base is void*, so you can simply set iov[0].iov_base = header;

    Note: This works only as long as your struct does not contain virtual functions, base classes, etc. (thanks, Timo).

    EDIT2

    In order to get {0x81, 0x05} in your struct, you should change the order of structure elements as follows:

    struct Bitmask {
        unsigned char opcode: 4; 
        unsigned char rsv3: 1; 
        unsigned char rsv2: 1; 
        unsigned char rsv1: 1; 
        unsigned char fin: 1; 
        unsigned char payload_length: 7; 
        unsigned char mask: 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider I have the following struct : struct IDirect3D { IDirect3D() : ref_count_(0) {}
I have a type which I consider use it as struct. It represents single
Consider I have the following text in a UILabel (a long line of dynamic
Consider I have the following interface: public interface A { public void b(); }
Consider I have an anchor which looks like this <div class=res> <a href=~/Resumes/Resumes1271354404687.docx> ~/Resumes/Resumes1271354404687.docx
Suppose I have a struct containing a std::string, like this: struct userdata{ int uid;
Consider the following snippet: struct Foo { static const T value = 123; //Where
In our code we used to have something like this: *(controller->bigstruct) = ( struct
If I have something like this in my code: void f(struct foo *x, struct
Consider this piece of code: struct Trade { float Price; char* time; int shares;

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.