header in socket programming
I'm programming my first sock开发者_高级运维et lines, and have accomplish to make a client server system that transfer messages between. The next step is to make a header sow the receiver know how much data the message is and how the message is to. How can I accomplish this?
I want the header to contain two int:
int to_phone_number;
int size;
How can is send a header ?
send(sock, the_message, max_message_length, 0);
Header, body, it's still data.
You will send your header the same way you'd send anything.
You'd probably want to have a struct message_header
that would compose your header.
struct message_header {
int to_phone_number;
int size;
};
Create a struct message_header
variable, set its fields, then send it.
struct message_header header;
// ...
send(sock, &header, sizeof(header), 0);
精彩评论