使用 public ip 连接到服务器时出现 SocketException 'the connected party did not properly respond after a period of time' (C#)
SocketException 'the connected party did not properly respond after a period of time' when connecting to server using public ip (C#)
我正在尝试使用 C# 创建服务器。目前,我有两个程序,一个服务器和一个客户端,当它们都在同一台计算机上 运行 时,它们都可以正常工作,服务器的 TcpListener
是使用 ip 127.0.0.1 (localhost) 创建的,并且客户端连接到 ip 127.0.0.1。但是,当我尝试使用我的 public ip 连接到服务器时,客户端程序等待大约 20 秒然后我得到一个 SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
我已经转发了我正在使用的端口(端口是2345)。我在另一台计算机上尝试了与上述相同的步骤,但仍然得到相同的结果。我在创建服务器的 TcpListener
时尝试使用 public ip 但后来我得到了 SocketException: The requested address is not valid in its context
所以我假设我应该只使用 127.0.0.1 (虽然我可能是错的,这是我第一次尝试这样的事情。
这是创建服务器的函数。
public static void RunServer()
{
string ipStr = "";
Console.WriteLine("Enter IP...");
ipStr = Console.ReadLine();
if (ipStr == "localhost")
ipStr = "127.0.0.1";
IPAddress ip = IPAddress.Parse(ipStr);
int port = -1;
Console.WriteLine("Enter port...");
while (port == -1)
{
try
{
port = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Please enter an integer.");
}
}
Console.WriteLine("Starting server on " + ip + ":" + port);
TcpListener tcpListener;
try
{
tcpListener = new TcpListener(ip, port);
tcpListener.Start();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e);
Console.ReadLine();
return;
}
while (true)
{
try
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream = tcpClient.GetStream();
StreamReader sr = new StreamReader(networkStream);
string msg = sr.ReadToEnd();
Console.WriteLine("Received message: \"" + msg + "\"");
sr.Close();
networkStream.Close();
tcpClient.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e);
}
}
}
以及创建客户端的函数。
public static void RunClient()
{
string ip = "";
Console.WriteLine("Enter IP...");
ip = Console.ReadLine();
int port = -1;
Console.WriteLine("Enter port...");
while (port == -1)
{
try
{
port = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Please enter an integer.");
}
}
while (true)
{
Console.WriteLine("Enter message...");
string msg = Console.ReadLine();
Console.WriteLine("Sending message: \"" + msg + "\"");
TcpClient tcpClient;
try
{
tcpClient = new TcpClient(ip, port);
}
catch (SocketException e)
{
Console.WriteLine("Could not connect to server. Connection refused.");
Console.WriteLine("Exception: " + e);
continue;
}
NetworkStream networkStream = tcpClient.GetStream();
networkStream.Write(Encoding.ASCII.GetBytes(msg), 0, msg.Length);
Console.WriteLine("Sent message!");
networkStream.Close();
tcpClient.Close();
}
}
我认为我目前拥有的东西可以工作,但我得到的只是那个例外。
这是因为您计算机的防火墙阻止了连接。在防火墙的入站规则中为您正在使用的端口添加例外。
问题是服务器程序的行 IPAddress ip = IPAddress.Parse(ipStr);
应该是 IPAddress ip = IPAddress.Any;
我正在尝试使用 C# 创建服务器。目前,我有两个程序,一个服务器和一个客户端,当它们都在同一台计算机上 运行 时,它们都可以正常工作,服务器的 TcpListener
是使用 ip 127.0.0.1 (localhost) 创建的,并且客户端连接到 ip 127.0.0.1。但是,当我尝试使用我的 public ip 连接到服务器时,客户端程序等待大约 20 秒然后我得到一个 SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
我已经转发了我正在使用的端口(端口是2345)。我在另一台计算机上尝试了与上述相同的步骤,但仍然得到相同的结果。我在创建服务器的 TcpListener
时尝试使用 public ip 但后来我得到了 SocketException: The requested address is not valid in its context
所以我假设我应该只使用 127.0.0.1 (虽然我可能是错的,这是我第一次尝试这样的事情。
这是创建服务器的函数。
public static void RunServer()
{
string ipStr = "";
Console.WriteLine("Enter IP...");
ipStr = Console.ReadLine();
if (ipStr == "localhost")
ipStr = "127.0.0.1";
IPAddress ip = IPAddress.Parse(ipStr);
int port = -1;
Console.WriteLine("Enter port...");
while (port == -1)
{
try
{
port = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Please enter an integer.");
}
}
Console.WriteLine("Starting server on " + ip + ":" + port);
TcpListener tcpListener;
try
{
tcpListener = new TcpListener(ip, port);
tcpListener.Start();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e);
Console.ReadLine();
return;
}
while (true)
{
try
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream networkStream = tcpClient.GetStream();
StreamReader sr = new StreamReader(networkStream);
string msg = sr.ReadToEnd();
Console.WriteLine("Received message: \"" + msg + "\"");
sr.Close();
networkStream.Close();
tcpClient.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e);
}
}
}
以及创建客户端的函数。
public static void RunClient()
{
string ip = "";
Console.WriteLine("Enter IP...");
ip = Console.ReadLine();
int port = -1;
Console.WriteLine("Enter port...");
while (port == -1)
{
try
{
port = int.Parse(Console.ReadLine());
}
catch (FormatException)
{
Console.WriteLine("Please enter an integer.");
}
}
while (true)
{
Console.WriteLine("Enter message...");
string msg = Console.ReadLine();
Console.WriteLine("Sending message: \"" + msg + "\"");
TcpClient tcpClient;
try
{
tcpClient = new TcpClient(ip, port);
}
catch (SocketException e)
{
Console.WriteLine("Could not connect to server. Connection refused.");
Console.WriteLine("Exception: " + e);
continue;
}
NetworkStream networkStream = tcpClient.GetStream();
networkStream.Write(Encoding.ASCII.GetBytes(msg), 0, msg.Length);
Console.WriteLine("Sent message!");
networkStream.Close();
tcpClient.Close();
}
}
我认为我目前拥有的东西可以工作,但我得到的只是那个例外。
这是因为您计算机的防火墙阻止了连接。在防火墙的入站规则中为您正在使用的端口添加例外。
问题是服务器程序的行 IPAddress ip = IPAddress.Parse(ipStr);
应该是 IPAddress ip = IPAddress.Any;