c - 在 linux 中使用 send() 函数发送数据

标签 c linux sockets send

我阅读了关于 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/

相关文章:

Linux 删除文件批处理脚本

c# - StreamReader.ReadLine 和 CR

c - 为什么我的 sig_int() 函数不能阻止我的函数在 c 中退出?

c - 为什么我的 C 程序的输出不正确?

android -/proc/net/tcp和/proc/pid/net/tcp有什么区别

c - linux gpio 驱动程序无法导出 GPIO

Linux 套接字 UDP 服务器 - 在 2 台机器上的 2 个服务器之间交换消息

node.js - 套接字,从服务器向服务器发出事件

将应用程序与设备驱动程序连接

c - 释放一系列链表