TcpListener.Start() 上的 SocketException [英] SocketException on TcpListener.Start()

查看:82
本文介绍了TcpListener.Start() 上的 SocketException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个基本的客户端/服务器程序,但是当我启动 TcpListener 时,它给了我 SocketException:请求的地址在其上下文中无效.
我实际上有一个方法可以返回我的公共 IP,并且它匹配 ipconfig 结果,因此下面的 IP 地址字符串不可能是问题.当然,出于安全原因,下面显示的 IP 不是我的真实 IP.我打开了下面的端口供一般使用.
无论如何,在上下文中无效是模糊的,所以我不确定这意味着什么.

I'm trying to make a basic client/server program, but when I start the TcpListener it gives me SocketException:The requested address is not valid in its context.
I actually have a method that returns my public IP, and it matches ipconfig results, so the IP address string below can't be the problem. Of course, the IP shown below isn't my real IP for security reasons. I opened the port below for general use.
Anyway, Not valid in context is vague, so I'm not sure what that means.

这是我的代码(用于 TcpListener):

Here's my code (for the TcpListener):

ServerIn = new TcpListener(IpAddress.Parse("100.100.100.100"), 8000);
ServerIn.Start();

提前致谢.

推荐答案

TcpListener 只能绑定到运行它的计算机的本地 IP 地址.所以你指定的 IP 不是本地机器的 IP.您的公共 IP 与本地计算机的 IP 不同,尤其是在您使用某种 NAT 时.

The TcpListener can only be bound to a local IP Address of the computer that runs it. So the IP you're specifying isn't an IP of the local machine. Your public IP isn't the same IP as your local machine, especially if you're using some kind of NAT.

如果我没记错的话,通常只是将 IPAddress.Any 作为您的 IP 来初始化侦听器.

If I recall correctly, it's common to just do IPAddress.Any as your IP to initialise the listener.

这篇关于TcpListener.Start() 上的 SocketException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆