代码之家  ›  专栏  ›  技术社区  ›  Ammar Muhi ud din

当从其他计算机侦听数据时,请求的地址在其上下文中无效

  •  1
  • Ammar Muhi ud din  · 技术社区  · 6 年前

    您好,我正在使用TCP IP通信,使用C当我的代码从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是在机器发送结果的机器中配置的。实际上,我已经看到了许多相同问题的答案,但我不知道在哪里更改IP地址。任何其他问题,任何人都可以给我答案,通过编写代码如何通过TCP IP协议与机器通信谢谢。

    1 回复  |  直到 6 年前
        1
  •  1
  •   TheGeneral    6 年前

    基本上,这个错误

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

    意思是 IP地址 给定的不被计算机上的任何网络接口使用。简而言之,你可能只需要使用 IPAddress.Any

    外延

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

    进一步阅读

    TcpListener Constructor (IPAddress, Int32)

    初始化侦听的tcplistener类的新实例 在指定的本地IP地址和 端口号。 评论 如果不关心分配了哪个本地地址,请指定 IPAddress.Any 对于localaddr参数和基础服务 提供程序将分配最合适的网络地址

    IP地址。任何

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