Winsock2 send()永远有超时块。 [英] Winsock2 send() with timeout blocks forever.

查看:124
本文介绍了Winsock2 send()永远有超时块。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的winsock错误。 SO_SNDTIMEO无法正常工作。

Possible winsock bug. SO_SNDTIMEO not working.

我有一个可以发送非阻塞套接字的库,但是当它无法发送完整数据包时,它会将套接字更改为阻塞并使用500ms超时之前的发送然后放弃并移除插座,开始新的连接。

I have a library that sends on non-blocking sockets but when it fails to send a full packet, it changes the socket to blocking and uses a send with 500ms timeout before then giving up and removing the socket, starting a fresh connection.

发送被永久阻止,SO_SENDTIMEO似乎完全被忽略。

The send is blocking forever, the SO_SENDTIMEO seems to be completely ignored.

Q.套接字是否可以在连接时从非阻塞转换为阻塞并在连接后的某个时间超时?

Q. Can a socket be converted from non-blocking at connection time to blocking with timeout sometime after it is connected?

ie

unsigned long arg = 0 ;

  result = ioctlsocket(sock,FIONBIO,& arg)== 0;

unsigned long arg = 0;
  result = ioctlsocket(sock, FIONBIO, &arg) == 0;

struct timeval tv;

    tv.tv_sec = writeTimeoutInMilliseconds / 1000;

    tv.tv_usec =(writeTimeoutInMilliseconds%1000)* 1000;

    setsockopt(sock,SOL_SOCKET,SO_SNDTIMEO,(char *)& tv,sizeof tv);

struct timeval tv;
    tv.tv_sec = writeTimeoutInMilliseconds/1000;
    tv.tv_usec = (writeTimeoutInMilliseconds%1000)*1000;
    setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof tv);

如果没有正常运行的超时,线程将被无限期阻塞,无法恢复。

Without a functioning timeout the thread is blocked indefinitely and cannot recover.

Wireshark已确认接收器,安全摄像头,已停止接收导致TCP零窗口和重复确认。

Wireshark has confirmed the receiver, a security camera, has stopped receiving causing TCP Zero Window and dup acks.

Wireshark还确认acks每10秒继续到达,保持"连接"状态。

Wireshark also confirms the acks continue to arrive every 10 seconds, keeping the "connection" alive.

如果我通过电源循环相机中断连接,阻止的发送返回并且线程继续。

If i break the connection by power cycling the camera, the blocked send returns and the thread continues.

关于SO_SNDTIMEO的文档可能不正确吗?也许它是连接状态而不是发送字节失败?

Could the documentation about SO_SNDTIMEO be incorrect? Perhaps it is state of connection and not failure to send bytes?

我找不到要搜索或提交错误的错误数据库。

I cannot find a bug database to search or to file a bug.

推荐答案

如果:

struct timeval tv;
tv.tv_sec = writeTimeoutInMilliseconds/1000;
tv.tv_usec = (writeTimeoutInMilliseconds%1000)*1000;
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof tv);

是你的应用程序的代码,然后我会问是否setsockopt函数调用实际上成功了吗?

is code from your application, then I would ask if the setsockopt function call actually succeeded?

setsockopt的文档说SO_SNDTIMEO的参数是一个DWORD,它是以毫秒为单位的超时值。 timeval结构是两个long,因此实际上是DWORD的两倍。

The documentation for setsockopt says that the parameter for SO_SNDTIMEO is a DWORD, which is the timeout value in milliseconds. The timeval structure is two longs, so it is actually double the size of a DWORD.


这篇关于Winsock2 send()永远有超时块。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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