我阅读了关于 send()
函数的文档,其中第三个参数 (len
) 是“The length, in bytes, of the data in buf 参数指向的缓冲区。
我似乎无法理解是它发送了我传递的字节数,还是我需要传递缓冲区的大小并发送其中包含的所有数据。
例子:
#define X 256
// main :
char test[X] = {0};
memcpy(test, "hello", 6);
send(sockfd, test, 6, 0)
send(sockfd, test, 256,0)
// will the first opetion send only hello? or hello000000....?
谢谢!
最佳答案
send
函数精确地发送您告诉它的字节数(假设它没有被中断并且不会以其他方式失败)。您需要发送的字节数由您正在实现的协议(protocol)决定。例如,如果协议(protocol)说你应该发送“FOO\r\n”,那么你需要发送 5 个字节。如果协议(protocol)指定整数以网络字节顺序表示为 4 个字节,而您要发送一个整数,则缓冲区应包含一个网络字节顺序的整数,您应该发送 4 个字节。缓冲区的大小对于 send
无关紧要。
关于c - 在 linux 中使用 send() 函数发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27201072/