【问题标题】:The requested address is not valid in its context when listen data from other machine从其他机器监听数据时,请求的地址在其上下文中无效
【发布时间】:2018-05-22 16:07:39
【问题描述】:

你好,当我的代码从 IP 侦听时,我正在使用 c# 使用 tcp ip 通信,然后它给我错误消息,请求的地址在其上下文中无效。这是代码。

 TcpListener server = null;
        try
        {
            // Set the TcpListener on port 13000.
            Int32 port = 5555;
            IPAddress localAddr = IPAddress.Parse("10.3.0.234");

            // TcpListener server = new TcpListener(port);
            server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            Byte[] bytes = new Byte[256];
            String data = null;

            // Enter the listening loop.
            while (true)
            {
                Console.Write("Waiting for a connection... ");

                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                TcpClient client = server.AcceptTcpClient();
                Console.WriteLine("Connected!");

                data = null;

                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();

                int i;

                // Loop to receive all the data sent by the client.
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    Console.WriteLine("Received: {0}", data);

                    // Process the data sent by the client.
                    data = data.ToUpper();

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                    Console.WriteLine("Sent Data:" + msg);
                    // Send back a response.
                  //  stream.Write(msg, 0, msg.Length);
                 //   Console.WriteLine("Sent: {0}", data);
                }

                // Shutdown and end connection
                client.Close();
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }
        finally
        {
            // Stop listening for new clients.
            server.Stop();
        }

实际上这个 ip 是在机器发送结果的机器中配置的。实际上我已经看到了很多相同问题的答案,但我不知道在哪里更改 IPAddress。任何人都可以通过编写代码来回答我如何通过 tcp ip 协议与机器通信谢谢。

【问题讨论】:

    标签: c# tcp tcpclient tcp-ip tcplistener


    【解决方案1】:

    基本上是错误

    请求的地址在其上下文中无效

    表示给定的IP 地址 未被您机器上的任何网络接口使用。简而言之,您可能只需要使用IPAddress.Any

    示例

    server = new TcpListener(IPAddress.Any, port);
    

    进一步阅读

    TcpListener Constructor (IPAddress, Int32)

    初始化 TcpListener 类的一个新实例,用于监听 对指定本地 IP 地址的传入连接尝试和 端口号。 备注 如果您不关心分配的本地地址,请指定 IPAddress.Any 用于 localaddr 参数,以及底层服务 提供商将分配最合适的网络地址

    IPAddress.Any

    提供指示服务器必须侦听的 IP 地址 所有网络接口上的客户端活动。

    【讨论】:

    • 好的,实际上当我使用此代码时,机器上没有可用状态你能告诉我,我可以发送连接请求吗?
    • then no available status on machine请多解释一下
    • 实际上,当我们启动此代码时,实验室机器上的状态会在 ip 和端口状态上发送结果更改为可用。当客户端和服务器之间建立任何连接时,其自动可用。机器是客户端,我的机器是服务器。
    猜你喜欢
    • 1970-01-01
    • 2022-01-02
    • 2020-12-25
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2017-11-20
    相关资源
    最近更新 更多