无法使用Socket连接到我的IP [英] Can't connect to my IP using Socket
问题描述
当我运行服务器时,服务器在此行中抛出此错误 尝试在服务器端反序列化空流 : this。 tcpListener.Start();
the server throws this error "Attempting to deserialize an empty stream" at server side in this line when I run the server: this.tcpListener.Start();
这是我的互联网IP,如果我使用我的本地IP,它可以工作。但我想要互联网IP。
This is my internet IP, if I use my local IP, it works. But i want the internet 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);
}
}
推荐答案
您启动应用程序并正在侦听端口,运行端口向前检查此处 。
When you start your application and are listening on a port, run a port forward check here.
我在编写网络软件时遇到了这个问题 - 这是一个常见的问题。
I've had this problem when writing network software - it's a common problem.
一旦您确认其端口转发问题,请转到 PortForward.com 。有很好的资源可以帮助您配置路由器。
Once you've verified that its a port forwarding problem, head over to PortForward.com. There are great resources there to help you configure your router.
这篇关于无法使用Socket连接到我的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!