简体   繁体   English

在Windows上获取套接字超时值

[英]Get a socket timeout value on windows

MS documentations for WinSock says you cant get timeout that you set for socket: WinSock的MS文档说您无法获得为套接字设置的超时:

The following table lists value for the optname that represent BSD socket options that are not supported by the getsockopt function. 下表列出了optname的值,这些值表示getsockopt函数不支持的BSD套接字选项。
... ...
SO_RCVTIMEO int Receives time-out. SO_RCVTIMEO int接收超时。 ... ...

but I have noticed that lovely .Net has that option: 但是我注意到,可爱的.Net有这样的选择:

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.receivetimeout.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1 http://msdn.microsoft.com/zh-CN/library/system.net.sockets.socket.receivetimeout.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1

So is there some ugly way to get it in native code. 因此,有一些丑陋的方法可以用本机代码获得它。 ?

The .NET design is not based on Windows implementation restrictions. .NET设计不基于Windows实现限制。 There's no magic here, trying to use it anyway will just throw a SocketException, "An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call". 这里没有魔术,无论如何尝试使用它只会抛出SocketException,“在getsockopt或setsockopt调用中指定了未知,无效或不受支持的选项或级别”。 A message that comes straight from Windows, WSAENOPROTOOPT winapi error code. 消息直接来自Windows,WSAENOPROTOOPT winapi错误代码。 But won't when your program runs on Linux, Apple or mobile operating system that does support the option. 但是当您的程序在支持该选项的Linux,Apple或移动操作系统上运行时,则不会。

So no, the fact that it has the option doesn't promise it will work. 所以不,它具有选项的事实并不保证它会起作用。

The first MSDN documentation I found says it is supported. 第一MSDN文档 ,我发现说,这支持的。 I was using it in Windows twenty years ago. 我二十年前在Windows中使用过它。 Something severely wrong with your first (uncited) source. 您的第一个(未注明出处)来源存在严重问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM