UDP 客户端服务器在同一台计算机上工作,但不能通过 LAN

发布于 2025-01-12 17:35:15 字数 2195 浏览 0 评论 0原文

我有一个简单的应用程序,它将连续的 UDP 数据包从服务器发送到客户端。如果服务器应用程序和客户端应用程序位于同一台计算机上,则一切正常,但当我将客户端移动到 LAN 中的另一台计算机时,它会停止处理消息 “所请求的地址在其上下文中无效”。我真的不明白为什么会发生这种情况。 这是服务器代码:

public void SendData()
{
    udpClient = new UdpClient();

    for (int i = 0; i < 8192; i++)
    {
        dataBytes[i] = Convert.ToByte(Convert.ToInt16(dataValues[i]));
    }

    udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

    try
    {
        udpClient.Connect(ipAddress, port);

        udpClient.Send(dataBytes, dataBytes.Length);
    }

    catch (Exception ex)
    {

    }
}

在客户端计算机上,这是代码:

private void receive()
{
            try
            {
                udpClient = new UdpClient();

                udpClient.Client.ReceiveTimeout = 1000;

                udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                int port = Convert.ToInt32("13064");
                IPAddress ipAddress = IPAddress.Parse("192.168.1.107");

                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
                socket = new UdpClient(localEndPoint);

                udpClient.Connect(ipAddress, port);

                socket.BeginReceive(new AsyncCallback(ReceivedDataCallback), socket);                   
            }

            catch (Exception ex)
            {
                if (!socketConnected)
                {
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
}

private void ReceivedDataCallback(IAsyncResult AR)
{
    try
    {
        socketConnected = true;

        clientSocket = AR.AsyncState as UdpClient;

        var port = Convert.ToInt32("13064");
        IPAddress ipAddress = IPAddress.Parse("192.168.1.107");

        IPEndPoint source = new IPEndPoint(ipAddress, port);

        nums = clientSocket.EndReceive(AR, ref source);

        OutputRemoteSamples();

        clientSocket.BeginReceive(new AsyncCallback(ReceivedDataCallback), clientSocket);
    }

    catch (Exception ex)
    {

    }
}

感谢您的任何建议 汤姆

I have a simple application that sends continuous UDP packets from a server to a client. Everything works well if the server app and client app are on the same computer but the minute I move the client to another computer in the LAN it stops working with a message
"the requested address is not valid in its context". I really don't understand why this is happening.
Here is the server code:

public void SendData()
{
    udpClient = new UdpClient();

    for (int i = 0; i < 8192; i++)
    {
        dataBytes[i] = Convert.ToByte(Convert.ToInt16(dataValues[i]));
    }

    udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

    try
    {
        udpClient.Connect(ipAddress, port);

        udpClient.Send(dataBytes, dataBytes.Length);
    }

    catch (Exception ex)
    {

    }
}

On the client machine here is the code:

private void receive()
{
            try
            {
                udpClient = new UdpClient();

                udpClient.Client.ReceiveTimeout = 1000;

                udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

                int port = Convert.ToInt32("13064");
                IPAddress ipAddress = IPAddress.Parse("192.168.1.107");

                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
                socket = new UdpClient(localEndPoint);

                udpClient.Connect(ipAddress, port);

                socket.BeginReceive(new AsyncCallback(ReceivedDataCallback), socket);                   
            }

            catch (Exception ex)
            {
                if (!socketConnected)
                {
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
}

private void ReceivedDataCallback(IAsyncResult AR)
{
    try
    {
        socketConnected = true;

        clientSocket = AR.AsyncState as UdpClient;

        var port = Convert.ToInt32("13064");
        IPAddress ipAddress = IPAddress.Parse("192.168.1.107");

        IPEndPoint source = new IPEndPoint(ipAddress, port);

        nums = clientSocket.EndReceive(AR, ref source);

        OutputRemoteSamples();

        clientSocket.BeginReceive(new AsyncCallback(ReceivedDataCallback), clientSocket);
    }

    catch (Exception ex)
    {

    }
}

Thanks for any suggestions
Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文