user265767
user265767

Reputation: 559

header in socket programming

I'm programming my first socket 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);

Upvotes: 2

Views: 4463

Answers (1)

Bertrand Marron
Bertrand Marron

Reputation: 22220

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);

Upvotes: 4

Related Questions