gpt4 book ai didi

c# - SendAsync 运行但数据并未真正发送

转载 作者:太空宇宙 更新时间:2023-11-03 16:21:46 27 4
gpt4 key购买 nike

我有一个使用 *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 的网络监视器进行检查,但没有发送此数据的日志。

发送方法非常简单:

 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

最佳答案

在与我们的网络管理员交谈后,我们发现问题是网络配置错误。我不“喜欢”网络,但问题与我正在通信的设备进入我们的 DMZ 这一事实有关。

关于c# - SendAsync 运行但数据并未真正发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13688078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com