You can send an integer as four bytes, of course (or no matter how many bytes your integer is); but you have to be careful. See the htonl() function provided on your system, probably in <arpa/inet.h> .
Assuming a four-byte integer n, this can do what you want:
uint32_t un = htonl(n); send(sockfd, &un, sizeof(uint32_t), flags);
You can then encode the add-on on the receiving side.
The reason for calling htonl() is that the high byte is usually transmitted first over the Internet, while some computer architectures, such as the ubiquitous x86, first store the low byte. If you do not correct the byte order, and the machine on one end is x86, and the machine on the other end is something else, then without htonl() integer will be distorted.
I am not an expert on this topic, by the way, but, in the absence of experimental answers, this answer can happen. Good luck.
source share