简体   繁体   中英

How to send messages as websocket client in c++ to a server?

I've a little problem in sendine my messages to the server. It probably send them and the server recieve them, but i have no real idea how to mask them correctly. I know this is actually the way: http://tools.ietf.org/html/rfc6455#section-5.2 But I'm not understanding a lot of it. It would be nice if someone could share a whole pseudocode what actually need to be done from the plain message until the writing into the buffer. I also searched here a lot for information but mostly they advice libs or directly the link. The problem is that libs are nice, but i've actually done already all i need. Only the sending mask is missing.

Regards

Can you send the message with mask bit off?

When mask bit is set, the server expect 4 bytes mask to present, and the payload is XOR'd with

for ( size_t i=0; i<packet_len; i++ ) {
    packet[i] = payload[i] ^ mask_bits[i&3];
}

EDIT: Adding more detail.

I assume you have done the Upgrade handshake, the actual bytes to send to network for communication are:

[flag][mask_bit, len][extended length][mask][payload]

Where:

  • flag: one byte, read the manual, note that I have not tried fragment.
  • mask bit, one bit located at highest bit of second byte.
  • len: 7 bits of second byte. If len is 126, real len is 16 bit field of extended length. if it is 127, extended length is 64-bit.
  • mask: present only if mask bit is set, it's 4 bytes using to XOR the payload.
  • payload: actual bytes to send

For example, to send "Hello" to server in binary mode, you do:

  • flag: 0x82, "Final packet in frame" and "Binary mode"
  • mask bit: 1
  • len: 6
  • mask: 0x11, 0x22, 0x33, 0x44
  • payload: "Hello", 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x00

The masked payload is: 0x48^0x11, 0x65^0x22, 0x6C^0x33, 0x6C^0x44, 0x6F^0x11, 0x00^0x22 => 0x59, 0x47, 0x5F, 0x28, 0x7E, 0x22

The whole stream is: 0x82, 0x86, 0x11, 0x22, 0x33, 0x44, 0x59, 0x47, 0x5F, 0x28, 0x7E, 0x22

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM