所以我尝试使用sendto()函数向客户端发送消息。在sendto()函数中发送一个结构 - C语言
和sendto()函数的原型是这样的:
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
但我想送一个结构作为第二个参数。我能做到吗?或者buf必须是一个字符串?
编辑:你看,我都能够发送这样的:
“的消息由‘标题’,其次是‘有效载荷’
你的消息头应该包括以下内容:
uint8_t version; /* must be 1. If you receive anything else, discard*/
uint8_t ttl; /* must be 1. If you receive anything else, discard*/
uint16_t payload_length; /* bytes following the header */
uint32_t account_identifier; /* digits of your account name */
uint32_t source_address; /* unused for now, set to 0 and ignore. */
uint32_t destination_address; /* unused for now, set to 0 and ignore. */
uint16_t checksum; /* unused for now, set to 0 and ignore. */
uint16_t protocol; /* must be 1. If you receive anything else, discard*/"
和有效载荷是一个文本字符串,如“你好”之类的。
我觉得像我送头将发送所有的结构的唯一方式信息。对?
雅互操作性不成问题。服务器的硬件是一样的。 – umdcoder 2014-10-19 21:01:15
带指针的结构体是否也能发送? – umdcoder 2014-10-19 21:01:41
@umdcoder不,它不是。一般来说,发送结构是一个糟糕的主意。不要使用结构作为网络协议。使用网络协议作为网络协议。 – EJP 2014-10-19 21:04:21