简体   繁体   English

当我尝试侦听端口时,请求的地址在其上下文中无效

[英]The requested address is not valid in its context when I try to listen a port

I am trying to connect to a sensor using network, the sensor's ip is 192.168.2.44 on port 3000;我正在尝试使用网络连接到传感器,传感器的 IP 是端口 3000 上的192.168.2.44

My Code:我的代码:

byte[] byteReadStream = null; // holds the data in byte buffer
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("192.168.2.44"), 3000);//listen on all local addresses and 8888 port
TcpListener tcpl = new TcpListener(ipe);
while (true)
{
    //infinite loop
    tcpl.Start(); // block application until data and connection
    TcpClient tcpc = tcpl.AcceptTcpClient();
    byteReadStream = new byte[tcpc.Available]; //allocate space
    tcpc.GetStream().Read(byteReadStream, 0, tcpc.Available);
    Console.WriteLine(Encoding.Default.GetString(byteReadStream) + "\n");
}

在此处输入图片说明

But when I run this code, I get this error:但是当我运行这段代码时,我收到了这个错误:

The requested address is not valid in its context请求的地址在其上下文中无效

Use IPAddress.Any to listen.使用IPAddress.Any进行侦听。 This seems to be your goal:这似乎是你的目标:

//listen on all local addresses

The listening address you have specified is invalid for some reason.由于某种原因,您指定的侦听地址无效。 There is no need to specify a numeric address.无需指定数字地址。

The TcpListener listens for connections from TCP network client, on a given port on your local machine . TcpListener在本地机器上的给定端口侦听来自 TCP 网络客户端的连接。 That is, for incoming connections.也就是说,对于传入连接。 Your code will be acting as a "server" of sorts.您的代码将充当某种“服务器”。

The requested address is not valid in its context请求的地址在其上下文中无效

Simply, it means that the IP address given is not used by any network interface on your machine.简而言之,这意味着您机器上的任何网络接口都没有使用给定的 IP 地址。

Use IPAddress.Any to listen on any IP address (ie network interface).使用IPAddress.Any侦听任何 IP 地址(即网络接口)。

However, it might be the case that you need to connect to the sensor (on port 3000), not the other way around.但是,您可能需要连接到传感器(在端口 3000 上),而不是相反。

EDIT: The new exception just tells you that you have two applications trying to listen to the same interface/port combination.编辑:新的异常只是告诉您有两个应用程序试图侦听相同的接口/端口组合。 Do you have two instances running at the same time?你有两个同时运行的实例吗?

One of the reason for this issue could be the presence of defaultProxy section in the configuration file which would be routing every outgoing call via proxy address mentioned in this setting.此问题的原因之一可能是配置文件中存在defaultProxy部分,该部分将通过此设置中提到的代理地址路由每个传出呼叫。 Ensure that either there is a proxy server/service listening at the proxy address, or comment this section to stop the routing.确保在代理地址上有代理服务器/服务侦听,或者注释此部分以停止路由。 In case the application configuration file does not have it, and this error is still there, check for the defaultProxy section in the machine.config file.如果应用程序配置文件没有它,并且此错误仍然存​​在,请检查machine.config文件中的defaultProxy部分。 machine.config should be available in folder C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Config . machine.config应该在文件夹C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Config 中可用 In our case it was the defaultProxy in machine.config .在我们的例子中,它是machine.config中的defaultProxy

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “请求的地址在其上下文中无效” SocketException - “The requested address is not valid in its context” SocketException 套接字错误请求的地址在其上下文中无效 - Socket Error The requested address is not valid in its context 获取请求的地址在其上下文中无效。 在我的聊天应用程序中 - Getting The requested address is not valid in its context. in my chat application WCF Tcp绑定“请求的地址在其上下文中无效”错误 - WCF Tcp Binding “The requested address is not valid in its context” Error SocketException:请求的地址在上下文中无效 - SocketException: The requested address is not valid in the context HttpListener:请求的地址在此上下文中无效 - HttpListener: The requested address is not valid in this context 为什么我的服务器 TcpListener 使用本地地址而不使用公共地址? - 例外:请求的地址在其上下文中无效 - Why is my Server TcpListener Working with Local Address but not with Public Address? - Exception: The requested address is not valid in its context 32feet .NET api拒绝连接“请求的地址在其上下文中无效” - 32feet .NET api refusing to connect “The requested address is not valid in its context” Socket.bind()在Win7上返回“请求的地址在其上下文中无效。” - Socket.bind() returns “The requested address is not valid in its context.” on Win7 面临错误 tcp 侦听器 SocketException:{0}System.Net.Sockets.SocketException (0x80004005):请求的地址在其上下文中无效 - Facing error tcp listener SocketException: {0}System.Net.Sockets.SocketException (0x80004005): The requested address is not valid in its context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM