0

我在 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)
        {
        }
    }
4

0 回答 0