TcpListener - 如何使用全局 IP 地址启动服务器 [英] TcpListener - how to start server with global ip address

查看:79
本文介绍了TcpListener - 如何使用全局 IP 地址启动服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会用我的全球 IP 地址启动 TcpListener 服务器.我有开放端口并使用 DMZ,我的端口 8074 可用,我应该能够启动此服务器.

I would start TcpListener server with my global IP address. I have open ports and using DMZ and my port 8074 is available and i should be able to start this server.

我的代码如下:

IPAddress ip = IPAddress.Parse("XX.XXX.XX.XXX.XX");
TcpListener server = new TcpListener(ip, Convert.ToInt32(8888));
TcpClient client = default(TcpClient);
try
{
    server.Start();
    Console.WriteLine("Server started...");
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
};

而且我一直有错误看起来像:

And all the time i have Error looks like:

Activated Event Time Duration 线程异常:一个异常是抛出:System.dll 中的System.Net.Sockets.SocketException"(The请求的地址在此上下文中不同").例外是抛出:System.dll 中的System.Net.Sockets.SocketException"(The请求的地址在这种情况下是不同的") 5.52s [5780] Worker线程

Activated Event Time Duration Thread Exception: An exception was thrown: "System.Net.Sockets.SocketException" in System.dll ("The requested address is different in this context"). An exception was thrown: "System.Net.Sockets.SocketException" in System.dll ("The requested address is different in this context") 5.52s [5780] Worker thread

您可以检查您是否希望此端口可在我的 IP 地址上访问但无法在此上启动服务器.

You can check if you want this port is accessible on my IP address but can't start server on this.

推荐答案

好的,谢谢 @jdweng .我需要改变的只是这条车道:

Yeah thanks @jdweng . All i need to change was just this lane :

IPAddress ip = IPAddress.Any;

这篇关于TcpListener - 如何使用全局 IP 地址启动服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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