2

一部のコンピューターでは、UdpClient.Send()の直後にUdpClient.Close()が呼び出された場合、UdpClientがデータを送信しないという奇妙な効果があります。パケット損失を確認するために.NET4.0とWireSharkを使用しています。

コーディングの重要な部分は次のとおりです。

UdpClient sender = new UdpClient();
sender.Connect( new IPEndPoint( this.ipAddress, this.Port ) );
int bytesSent = sender.Send( data, data.Length );
sender.Close();

奇妙なのは:

  • ほとんどのコンピューターでは、データは問題なく送信されます
  • パケットが送信されなくても例外やその他のエラーはありません
  • bytesSentは常にdata.Lengthと等しくなります
  • パケットを送信しないコンピューターでは、sender.Close()を呼び出す直前にThread.Sleep(250)で問題が解決します。

では、UdpClient.Send()が正しいバイト数を報告した後でも、何がパケットの送信をキャンセルできるでしょうか。これが特定のマシンでのみ発生するのはなぜですか?これは、一部のネットワークドライバ、ウイルス対策ソフトウェアなどの異なる動作である可能性がありますか?

また、LingerOptionsを明示的に設定しようとしましたが、デフォルトでは、基になるソケットを閉じる前にすべての保留中のデータを送信するため、これは不要です。ただし、sender.Client.LingerState = new LingerOption(true、10)を実行する場合(http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.lingerstate.aspxで説明されているように)取得します

 SocketException (0x80004005): An unknown, invalid, or unsupported option 
 or level was specified in a getsockopt or setsockopt call.

何が起こっているのかアイデアはありますか?

よろしく、セブン

4

1 に答える 1

2

OK, this has nothing to do with .NET and my software either. It seems that the virus scanner also scans the complete network trafic. And so .NET library functions for sending UDP packages actially did send the package, but the scanner discards it if UdpClient.Close() was called too soon after the Send() method.

So, there are two possible work-arounds now (that worked for me)

  1. Introduce a little sleep before calling UdpClient.Close() (about 4ms are sufficient)
  2. Remove the virus scanner and try another one (Microsoft Security Essentials does not show this effect)
于 2011-10-17T14:02:46.567 に答える