什么是 send() 和 recv() return?没有收到完整的消息,总是限制在一个 byte/8 个字符内?
What does send() and recv() return? Not getting full message, always limited to one byte/8 characters?
我正在 ubuntu linux 上用 C 编写一个简单的套接字示例。我知道 send()
和 recv()
函数不一定要等待 send/receive 整个消息。我正在尝试发送一个简单的 hello world 消息。我知道它的大小(至少我认为我知道),所以我想知道我可以将什么添加到我的 send()
和 recv()
实现中以等待已知的消息大小。
阅读 send()
和 recv()
的手册页说函数原型如下所示:ssize_t send(int sockfd, const void* buf, size_t len, int flags)
和 recv() 使用相同的原型。我的问题是它们 return - 字节数或字符数是多少?无论哪种方式,为什么它总是8?
在测试我的消息大小时,无论我使用 char* message = "test";
还是 char* message = "Hello world, I clearly do not understand what is going on here";
,sizeof() 运算符总是 returning 8
客户代码
fprintf(stdout, "client connected to %s:%d\n", hostname, portno);
printf("Size of %s is : %lu\n", message, sizeof(message));
printf("other size of %s is %lu, message, sizeof(message)/sizeof(char));
ssize_t written_bytes = send(socket_fd, message, sizeof(message),0);
assert(written_bytes == sizeof(message)); //Shouldn't the code fail here?
printf("written bytes: %lu\n", written_bytes);
我会添加服务器代码,但是如果它从不发送它为什么没有收到所有内容就没有意义了。
send
和 recv
returns 分别发送和接收的字节数 。当您使用 sizeof
运算符测试消息的大小时,您实际上是在计算指针的大小,即计算机上的 8
字节。
我正在 ubuntu linux 上用 C 编写一个简单的套接字示例。我知道 send()
和 recv()
函数不一定要等待 send/receive 整个消息。我正在尝试发送一个简单的 hello world 消息。我知道它的大小(至少我认为我知道),所以我想知道我可以将什么添加到我的 send()
和 recv()
实现中以等待已知的消息大小。
阅读 send()
和 recv()
的手册页说函数原型如下所示:ssize_t send(int sockfd, const void* buf, size_t len, int flags)
和 recv() 使用相同的原型。我的问题是它们 return - 字节数或字符数是多少?无论哪种方式,为什么它总是8?
在测试我的消息大小时,无论我使用 char* message = "test";
还是 char* message = "Hello world, I clearly do not understand what is going on here";
客户代码
fprintf(stdout, "client connected to %s:%d\n", hostname, portno);
printf("Size of %s is : %lu\n", message, sizeof(message));
printf("other size of %s is %lu, message, sizeof(message)/sizeof(char));
ssize_t written_bytes = send(socket_fd, message, sizeof(message),0);
assert(written_bytes == sizeof(message)); //Shouldn't the code fail here?
printf("written bytes: %lu\n", written_bytes);
我会添加服务器代码,但是如果它从不发送它为什么没有收到所有内容就没有意义了。
send
和 recv
returns 分别发送和接收的字节数 。当您使用 sizeof
运算符测试消息的大小时,您实际上是在计算指针的大小,即计算机上的 8
字节。