UDT::recv 函数

recv

The recv method reads certain amount of data into a local memory buffer.

int recv(
  UDTSOCKET  u,
  char*  buf,
  int  len,
  int  flags
);
Parameters
u
[in] Descriptor identifying a connected socket.
buf
[out] The buffer used to store incoming data.
len
[in] Length of the buffer.
flags
[in] Ignored. For compatibility only.

Description

The recv method reads certain amount of data from the protocol buffer. If there is not enough data in the buffer, recv only reads the available data in the protocol buffer and returns the actual size of data received. However, recv will never read more data than the buffer size indicates by len.

In blocking mode (default), recv waits until there is some data received into the receiver buffer. In non-blocking mode, recv returns immediately and returns error if no data available.

If UDT_RCVTIMEO is set and the socket is in blocking mode, recv only waits a limited time specified by UDT_RCVTIMEO option. If there is still no data available when the timer expires, error will be returned. UDT_RCVTIMEO has no effect for non-blocking socket.


以上是UDT文档的解释。


这里,在默认情况下:用SOCK_STREAM方式,接收为blocking mode,可以保证接收安顺序,完成的接收数据,但是一个大的数据包一层 UDT::send, 这里就很可能会分包按顺序发送,分包大小不定, 同理,也就是,接收端会按顺序不定大小的接收一个大的数据包。


所以,如果处理大数据,还是要自定义结构处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
UDT是一种基于UDP协议的数据传输协议,它提供了可靠的数据传输,支持流和数据报两种传输方式。UDT::connect()函数用于连接到指定的远程主机。 UDT::connect()函数的用法如下: ``` int UDT::connect(UDTSOCKET u, const struct sockaddr *name, int namelen); ``` 参数说明: - `u`:UDT套接字。 - `name`:指向要连接的远程主机的地址信息。 - `namelen`:地址信息的长度。 返回值: - 如果连接成功,返回0; - 如果连接失败,返回UDT_ERROR。 使用该函数前,需要先创建一个UDT套接字,并调用UDT::bind()函数绑定本地地址。连接成功后,可以使用UDT::send()和UDT::recv()函数进行数据传输。需要注意的是,连接成功后,不能再使用UDT::listen()函数UDT::accept()函数函数。 示例代码如下: ``` #include <udt.h> int main() { // 创建UDT套接字 UDTSOCKET sock = UDT::socket(AF_INET, SOCK_STREAM, 0); // 绑定本地地址 struct sockaddr_in my_addr; my_addr.sin_family = AF_INET; my_addr.sin_port = htons(9000); my_addr.sin_addr.s_addr = INADDR_ANY; memset(&(my_addr.sin_zero), 0, sizeof(my_addr.sin_zero)); UDT::bind(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)); // 连接远程主机 struct sockaddr_in serv_addr; serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(1234); serv_addr.sin_addr.s_addr = inet_addr("192.168.1.100"); memset(&(serv_addr.sin_zero), 0, sizeof(serv_addr.sin_zero)); int ret = UDT::connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); if(ret == UDT::ERROR) { // 连接失败 printf("connect error!\n"); UDT::close(sock); return -1; } // 连接成功,进行数据传输 char buf[1024]; int len = UDT::send(sock, buf, sizeof(buf), 0); len = UDT::recv(sock, buf, sizeof(buf), 0); // 关闭套接字 UDT::close(sock); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chinabinlang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值