c++ - Sending a struct with accidental wrong bytes between two computers -


i sending struct byte stream using tcp socket 1 system another. both systems little-endian , struct not have floating number. in summary structs in both systems have same size (the same padding/alignment done them both).

when receive byte stream, store in buffer , re-construct struct waveshortmessage following:

// receiving side waveshortmessage tmp; memcpy(&tmp, &buffer, sizeof(tmp)); 

everything works expected , can extract different variables of struct. question is, how can make sure byte stream in buffer belongs struct. in other words, there safety check program notifies user if received byte stream not belong waveshortmessage struct. particularly interested solution not involve changing source code in sender side.

there's nothing hidden in arbitrary sequence of bytes specifies these bytes "belong" to.

a sequence of bytes that: sequence of bytes. 8 bits in each byte. given number of bytes, in sequence. raw data.

if formal specifications of popular internet protocols http or smtp (they're available, google search away), find these high level network protocols consist of fleshed-out, structured communication conventions. email or web page not blasted across intertubes mysterious, amorphous bag of bytes.

these kinds of communications between 2 parties, whether send email, or web page, follows well-defined, step step procedure. connection made, , parties in question exchange various pleasantries on network connection, before getting down business, transferring data in question.

then, data follows well-defined structure. web pages coded in html, ancillary data css. email messages have well-defined structure: set of header lines, each header line following well-defined convention, blank line, content of message. , on.

so, when email message, or web page, sent between sender , receiver, neither 1 has doubt, or reason confused heck about.

you correct in feeling not satisfied blasting binary blob containing structure across socket connection, , hoping best. instead, need gather requirements, sit down, , on course of few minutes... or maybe few hours... or maybe days, , come protocol exchanging or transmitting information that's carried in binary structure.

and then, have implement whole thing.

then, sender , receiver have no doubt, or uncertainty, information that's being sent or received.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -