- Sender:
- First,
convert the
binary data to
network byte ordering
- Then, send the converted data
|
Example:
int k = 1; // k is stored in host byte order
printf("Sending: int value = %d\n", k);
int x = htonl(k); // Convert to network byte ordering
sendto(s, &x, sizeof(int), 0 /* flags */,
(struct sockaddr *)&dest_addr, sizeof(dest_addr));
|
- Receiver:
- Receiver the
binary data
(we know it is in
network byte order
- Convert the
data to
host byte order using:
int ntohl( int ): convert 4 byte data from network byte ordering
to host byte ordering
short ntohs( short): convert 2 byte data from network byte ordering
to host byte ordering
|
Example:
int x;
src_addr_len = sizeof(src_addr);
len = recvfrom(s, &x, sizeof(int), 0 /* flags */,
(struct sockaddr *) &src_addr, &src_addr_len);
k = ntohl( x ); // Convert int from network byte ordering
// to host byte ordering
printf("k = %d", k);
|
|
|