无法获得UDP响应

时间:2013-03-13 17:38:09

标签: c# asp.net udp

我在asp.net上创建了一个网站,供我公司监控不同的服务器,但现在我卡住了。

在一个函数中,我想要其中一个服务器的UDP响应时间,因为我能够发送UDP数据包但无法从服务器获得响应。服务器正确接收请求。我怎么能这样做?

以下是代码:

    byte[] byteData;  
    IPEndPoint ipeSender;
    EndPoint epSender;
    Socket serverSocket;
    IPEndPoint ipe;
    EndPoint eps;


    protected void Page_Load(object sender, EventArgs e)
    {

        byteData = new byte[1024];
        ipeSender = new IPEndPoint(IPAddress.Parse("192.168.4.25"), 2015);
        epSender = (EndPoint)ipeSender;
        serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
        serverSocket.Bind(epSender);
        byte[] byteData1 = Encoding.ASCII.GetBytes("3^12062012152936041^OARAREGIONXWEB^SDHS^1^0^40^3^0^8^Register^");


        serverSocket.SendTo(byteData1, eps);

        ipe = new IPEndPoint(IPAddress.Parse("70.88.142.138"), 2015);
        eps = (EndPoint)ipe;
        serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnNetworkBytesReceived), epSender);

    }


    private void OnNetworkBytesReceived(IAsyncResult ar)
    {
        try
        {
            IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
            EndPoint epSender = (EndPoint)ipeSender;
            if (serverSocket == null)
                return;
            //if(epSender.ToString()!= dw[1, RowIndex].Value.ToString())
            //  return; // serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnNetworkBytesReceived), epSender);

            int DataLength = serverSocket.EndReceiveFrom(ar, ref epSender);

            byte[] TempBytes = new byte[DataLength];
            System.Buffer.BlockCopy(byteData, 0, TempBytes, 0, DataLength);

            serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender, new AsyncCallback(OnNetworkBytesReceived), epSender);

        }
        catch (SocketException socexp)
        {

        }
        catch (ObjectDisposedException objexp)
        {
        }
        catch (Exception exp)
        {
        }
    }

0 个答案:

没有答案