我有一个使用 *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