如何使Winsock中的“发送"不阻塞 [英] How to make 'send' non-blocking in winsock

查看:58
本文介绍了如何使Winsock中的“发送"不阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个程序,该程序以固定的时间间隔将UDP数据包发送到服务器,如下所示:

I am making a program which sends UDP packets to a server at a fixed interval, something like this:

while (!stop) {
    Sleep(fixedInterval);

    send(sock, pkt, payloadSize, flags);
 }

但是不能保证周期性,因为 send 是阻塞调用(例如,当 fixedInterval 为20ms,而对 send 的调用为> 20ms).您知道如何将 send 转换为非阻塞操作吗?

However the periodicity cannot be guaranteed because send is a blocking call (e.g., when fixedInterval is 20ms, and a call to send is > 20ms ). Do you know how I can turn the send into a non-blocking operation?

推荐答案

API ioctlsocket可以做到.您可以按以下方式使用它,但是为什么不在winsock中使用I/O模型呢?

The API ioctlsocket can do it.You can use it as below.But why don't you use I/O models in winsock?

ioctlsocket(hsock,FIOBIO,(unsigned long *)& ul);

这篇关于如何使Winsock中的“发送"不阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆