send()
and recv()
don't send strings. They send bytes. It is up to you to provide an interpretation of the bytes that makes sense.
If you wish to send integers, you need to decide what size integers you are going to send -- 1 byte, 2 bytes, 4 bytes, 8 bytes, etc. You also need to decide on an encoding format. "Network order" describes the convention of Big-Endian formatting. You can convert to and from network order using the following functions from <arpa/inet.h>
or <netinet/in.h>
:
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
If you stick to using htonl()
before sending (host-to-network, long) and ntohl()
after receiving (network-to-host, long), you'll do alright.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…