当我尝试监听端口时,请求的地址在其上下文中无效 [英] The requested address is not valid in its context when i try to listen a port
本文介绍了当我尝试监听端口时,请求的地址在其上下文中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用网络连接到传感器,传感器的ip是端口3000上的192.168.2.44
;
I am trying to connect to a sensor using network, the sensor's ip is 192.168.2.44
on port 3000;
我的代码:
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 got this error:
请求的地址在其上下文中无效
The requested address is not valid in its context
推荐答案
使用IPAddress.Any
进行收听.这似乎是您的目标:
Use IPAddress.Any
to listen. 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.
这篇关于当我尝试监听端口时,请求的地址在其上下文中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文