[英]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.