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

时间:2012-12-03 16:58:52

标签: c# asyncsocket sendasync

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

虽然跟踪显示数据已发送(并且发生了接收超时),但数据并未真正发送。我已经使用微软的网络监视器进行了检查,并且没有发送此数据的日志。

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

1 个答案:

答案 0 :(得分:0)

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