* Asyncメソッド(SendAsyncなど)を使用してソケットを介して通信するアプリケーションがあります。SendAsyncが実行されても、データがネットワーク経由で送信されないという奇妙な動作が発生します。
ネットワークトレースを有効にして、次のログを取得しました。
System.Net.Sockets Verbose: 0 : [4300] Socket#5024928::SendAsync()
System.Net.Sockets Verbose: 0 : [4300] Socket#5024928::SendAsync(Boolean#1)
System.Net.Sockets Verbose: 0 : [7320] Data from Socket#5024928::FinishOperation(SendAsync)
System.Net.Sockets Verbose: 0 : [7320] 00000E8C : 01 99 27 00 00 00 01 00-00 00 00 00 00 00 00 00 : ..'.............
System.Net.Sockets Verbose: 0 : [7320] 00000E9C : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [7320] 00000EAC : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [7320] 00000EBC : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [7320] 00000ECC : 00 00 B3 F4 : ....
System.Net.Sockets Verbose: 0 : [7320] Socket#5024928::ReceiveAsync()
System.Net.Sockets Verbose: 0 : [7320] Exiting Socket#5024928::ReceiveAsync() -> Boolean#1
System.Net.Sockets Error: 0 : [7320] Socket#5024928::UpdateStatusAfterSocketError() - TimedOut
トレースはデータが送信された(そして受信タイムアウトが発生した)ことを示していますが、データは実際には送信されていません。Microsoftのネットワークモニターで確認しましたが、このデータが送信されたログはありません。
sendメソッドは非常に簡単です。
public void Send(AsyncClientState state, byte[] data) {
var socket = state.Socket;
var dataTransferredEventArgs = new DataTransferredEventArgs(data, _maxBytes) { State = state };
state.Events.RaiseBeforeSend(dataTransferredEventArgs);
byte[] bytesToSend = dataTransferredEventArgs.Bytes.ToArray();
SocketAsyncEventArgs args = GetSocketAsyncEventArgsArgs(socket, bytesToSend.Length);
if (args == null) {
state.Events.RaiseTimeout(Operation.Send);
return;
}
args.Completed += SendCompleted;
args.UserToken = state;
Buffer.BlockCopy(bytesToSend, 0, args.Buffer, args.Offset, bytesToSend.Length);
bool sendAsync = socket.SendAsync(args);
if (!sendAsync) {
SendCompleted(this, args);
}
}
private void SendCompleted(object sender, SocketAsyncEventArgs e) {
var state = (AsyncClientState) e.UserToken;
var events = state.Events;
try {
if (e.SocketError != SocketError.Success) {
events.RaiseError(new ErrorEventArgs("(SendSocketError)" + e.SocketError));
return;
}
if (e.LastOperation == SocketAsyncOperation.Send) {
e.Completed -= SendCompleted;
_asyncEventArgsPool.PutBack(e);
events.RaiseAfterSend(new StateEventArgs(state));
}
}
catch (ObjectDisposedException) { }
catch (SocketException ex) {
events.RaiseError(new ErrorEventArgs(string.Format("{0}: {1}", ex.SocketErrorCode, ex.Message)));
}
}
ここで何かが足りませんか?ネットワークトレースは本当に信頼できますか?
アップデート
Begin / End *を使用しようとしましたが、それでも同じ問題が発生します。トレースは次のとおりです。
System.Net.Sockets Verbose: 0 : [6632] Socket#29357909::BeginSend()
System.Net.Sockets Verbose: 0 : [6632] Exiting Socket#29357909::BeginSend() -> OverlappedAsyncResult#30136159
System.Net.Sockets Verbose: 0 : [6500] Data from Socket#29357909::PostCompletion
System.Net.Sockets Verbose: 0 : [6500] 00000000 : 01 99 27 00 00 00 01 00-00 00 00 00 00 00 00 00 : ..'.............
System.Net.Sockets Verbose: 0 : [6500] 00000010 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [6500] 00000020 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [6500] 00000030 : 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 : ................
System.Net.Sockets Verbose: 0 : [6500] 00000040 : 00 00 B3 F4 : ....
System.Net.Sockets Verbose: 0 : [6500] Socket#29357909::EndSend(OverlappedAsyncResult#30136159)
System.Net.Sockets Verbose: 0 : [6500] Exiting Socket#29357909::EndSend() -> Int32#68
System.Net.Sockets Verbose: 0 : [6500] Socket#29357909::BeginReceive()
System.Net.Sockets Verbose: 0 : [6500] Exiting Socket#29357909::BeginReceive() -> OverlappedAsyncResult#63504289
System.Net.Sockets Verbose: 0 : [6500] Socket#29357909::EndReceive(OverlappedAsyncResult#63504289)
System.Net.Sockets Error: 0 : [6500] Socket#29357909::UpdateStatusAfterSocketError() - TimedOut
System.Net.Sockets Error: 0 : [6500] Exception in Socket#29357909::EndReceive - A connection attempt failed because the connected party did not properly respond after a period of time or established connection failed because connected host has failed to respond.
System.Net.Sockets Verbose: 0 : [6500] Exiting Socket#29357909::EndReceive() -> Int32#0