无法使用Socket连接到我的IP

当我运行服务器时,服务器在此行的服务器端抛出此错误“试图反序列化空流”this.tcpListener.Start();

这是我的互联网IP,如果我使用我的本地IP,它可以工作。 但我想要互联网IP。

客户端:

 TcpClient tcpclnt = new TcpClient(); Console.WriteLine("Connecting....."); tcpclnt.Connect("187.115.131.44", 8001); 

服务器端:

  public Server() { try { IPAddress ip = IPAddress.Parse("187.115.131.44"); tcpListener = new TcpListener(ip, 8001); listenThread = new Thread(new ThreadStart(ListenForClients)); listenThread.Start(); } catch (Exception e) { Console.WriteLine("Error..... " + e.StackTrace); Console.ReadKey(); } } private void ListenForClients() { this.tcpListener.Start(); while (true) { //blocks until a client has connected to the server TcpClient client = this.tcpListener.AcceptTcpClient(); //create a thread to handle communication //with connected client Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm)); clientThread.Start(client); } } 

当您启动应用程序并正在侦听端口时,请在此处运行端口转发检查。

我在编写网络软件时遇到过这个问题 – 这是一个常见的问题。

一旦您确认其端口转发问题,请转到PortForward.com 。 有很多资源可以帮助您配置路由器。

您明显落后的防火墙和/或路由器/交换机是原因。 它需要在所述设备上更改或添加NAT策略。

我有同样的问题,我也在路由器和防火墙后面。 我发现使用你的代码localhost(127.0.0.1)是完美的,但是一旦我们尝试连接到远程主机,它就会出错。 解决方案是更改服务器端代码,如下所示:

 tcpListener = new TcpListener(IPAddress.Any, 8001); 

希望对你有帮助…