如何调试网络错误(特别是ICMP)

时间:2012-02-12 19:33:08

标签: c# windows-phone-7 networking udp

我有一个服务器/客户端类型应用程序,Wireshark显示客户端已将数据包发送到服务器,服务器已给出预期响应但显示ICMP目标端口无法访问错误。

我正在使用MDSN网站上的一个功能,该功能以前对我有用。

编辑:要更新我已经检查过手机开始收听后正在发送数据包,我已经尝试了其他端口。没有套接字异常,所以我只是在寻找调试网络错误的最佳方法。

有什么想法吗?

public string Receive()
    {
        string response = "Operation Timeout";



        // We are receiving over an established socket connection
        if (udpSocket != null)
        {
            // Create SocketAsyncEventArgs context object
            SocketAsyncEventArgs socketEventArg = new SocketAsyncEventArgs();
            socketEventArg.RemoteEndPoint = new DnsEndPoint(SERVER, RECIVEPORT);

            // Setup the buffer to receive the data
            socketEventArg.SetBuffer(new Byte[MAX_BUFFER_SIZE], 0, MAX_BUFFER_SIZE);

            // Inline event handler for the Completed event.
            // Note: This even handler was implemented inline in order to make this method self-contained.
            socketEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(delegate(object s, SocketAsyncEventArgs e)
            {
                if (e.SocketError == SocketError.Success)
                {
                    // Retrieve the data from the buffer
                    response = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);
                    response = response.Trim('\0');
                }
                else
                {
                    response = e.SocketError.ToString();

                }

                _clientDone.Set();
            });

            // Sets the state of the event to nonsignaled, causing threads to block
            _clientDone.Reset();

            // Make an asynchronous Receive request over the socket
            Debug.WriteLine("Listening now:" + DateTime.Now.Second + ":" + DateTime.Now.Millisecond);
            try
            {
                Debug.WriteLine("No socket exception");
                udpSocket.ReceiveFromAsync(socketEventArg);
            }
            catch (SocketException e)
            {
                Debug.WriteLine(e.SocketErrorCode);
            }

            // Block the UI thread for a maximum of TIMEOUT_MILLISECONDS milliseconds.
            // If no response comes back within this time then proceed
            _clientDone.WaitOne(TIMEOUT_MILLISECONDS);
        }
        else
        {
            response = "Socket is not initialized";
        }

        return response;
    }

1 个答案:

答案 0 :(得分:0)

“ICMP目标端口无法访问”表示没有应用程序绑定到您发送到的端口。确保sendto()的目标是更正IP地址和端口号。还要检查您的侦听器是否正在bind()上调用INADDR_ANY以及正确的端口。一个常见的错误是忘记将端口号转换为网络字节顺序(big-endian)。请参阅htons()