最可移植的方法是自己將字節組裝到緩沖區中: uint8_t buf[64]; // make sure this is large enough to hold the bytesuint8_t * b = buf;uint32_t neDist = htonl(msg.dist); // send multi-byte values in big-endian formmemcpy(b, &neDist, sizeof(neDist));b += sizeof(neDist);memcpy(b, &msg.num, sizeof(msg.num));b += sizeof(msg.num);uint32_t neAddr = htonl(msg.addr.s_addr);memcpy(b, &neAddr, sizeof(neAddr));b += sizeof(neAddr);uint32_t numBytesToSend = (b-buf);sendto(sockfd, buf, numBytesToSend, 0, (struct sockaddr*) &server_address, sizeof(server_address)); ... 在接收端,比如: uint8_t buf[64]; // make sure this is lar