【问题标题】:Socket.SetSocketOption throws invalid argument exception on Mac OS & Linux ,but works fine on WindowsSocket.SetSocketOption 在 Mac OS 和 Linux 上抛出无效参数异常,但在 Windows 上工作正常
【发布时间】:2019-10-28 23:00:30
【问题描述】:
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeout);

如果超时设置为 -1,则该方法在 Windows 上运行良好,但在 Linux 和 Mac 上失败,并出现以下异常

System.Net.Sockets.SocketException (22):参数无效 System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError 错误,字符串 callerName) 在 System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue, Boolean 静默)在 System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue)

如果mac和linux上超时设置为0,也不例外。

1) 如果超时设置为 -1,它会在 Windows 上进行无限轮询。 timeout=0 会在 Windows 上做同样的事情吗?

2) 如果超时设置为 0,则在 linux 上运行?它会进行无限期的民意调查吗?

当我使用 0 超时时,应用程序会间歇性地失败

    System.NotSupportedException: The BeginWrite method cannot be called >when another write operation is pending.
      at System.Net.Security._SslStream.ProcessWrite(Byte[] buffer, >Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
      at System.Net.Security._SslStream.BeginWrite(Byte[] buffer, Int32 >offset, Int32 count, AsyncCallback asyncCallback, Object asyncState)

【问题讨论】:

  • 向 Microsoft 提交错误。该行为与文档不符。
  • 错误信息一目了然。 “当另一个写入操作挂起时,无法调用 BeginWrite 方法。” - BeginWrite() 用于异步写入。一次只能运行一个写入操作。这与阅读无关。在发出新的写入之前,您必须等待写入完成。请显示您的实际代码,因为您在其中做错了与SetSocketOption() 无关的事情

标签: c# .net linux sockets .net-core


【解决方案1】:

1) 如果超时设置为 -1,它会在 Windows 上进行无限轮询。将要 timeout=0 在windows上做同样的事情吗?

2) 如果超时设置为 0,则在 linux 上运行?它会进行无限期的民意调查吗?

是的,根据Socket.RecieveTimeout 的文档,0 和 -1 的值都具有相同的行为

超时值,以毫秒为单位。默认值为 0,即 表示无限超时。指定 -1 还表示 无限超时。

【讨论】:

  • 谢谢。但是在 Windows 上,如果该值设置为 0,应用程序会间歇性失败并出现异常(不能同时在套接字上进行读写)我的是多线程的应用。所以想确认 0 & -1 是否执行相同的操作
  • @subbaraoc "在 Windows 上,如果该值设置为 0,应用程序会间歇性地失败并出现异常(不能同时在套接字上进行读取和写入之类的操作) i>" - 这不是一个真正的错误,并且绝对允许同时读取和写入,因为套接字是双向的。什么是“真正的”错误信息?您在代码中做错了其他事情,但您没有显示您的代码
  • @RemyLebeau 我已经更新了这个问题,但有例外
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 2021-03-27
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
相关资源
最近更新 更多