"SocketException: No such host is known" 连接到本地主机上的端口时
"SocketException: No such host is known" when connecting to a port on local host
我知道 Whosebug 上已经有几个问题询问这个特定的异常,但我还没有找到解决我的问题的答案。
这里是相关的代码片段:
public static class Server
{
public const string LocalHost = "http://127.0.0.1";
public const int Port = 31311;
public static readonly string FullAddress = $"{LocalHost}:{Port}";
private static readonly TimeSpan RetryConnectionInterval = TimeSpan.FromSeconds(10);
public static async Task AwaitStart()
{
try
{
TcpClient tcpClient = new TcpClient();
ConnectionState connectionState = new ConnectionState(tcpClient);
tcpClient.BeginConnect(
host: HostAddress,
port: Port,
requestCallback: PingCallback,
state: connectionState);
bool startedSuccessfully = connectionState.IsSuccess;
while (!startedSuccessfully)
{
await Task.Delay(RetryConnectionInterval);
startedSuccessfully = connectionState.IsSuccess;
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
private static void PingCallback(IAsyncResult result)
{
ConnectionState state = (ConnectionState)result.AsyncState;
try
{
state.TcpClient.EndConnect(result);
state.IsSuccess = true;
Console.WriteLine("The server is successfully started.");
}
catch (SocketException)
{
Console.WriteLine($"The server is not yet started. Re-attempting connection in {RetryConnectionInterval.Seconds} seconds.");
Wait(RetryConnectionInterval).GetAwaiter().GetResult();
state.TcpClient.BeginConnect(host: HostAddress, port: Port, requestCallback: PingCallback, state: state);
}
}
private static async Task Wait(TimeSpan duration)
{
await Task.Delay(duration);
}
}
public class ConnectionState
{
public bool IsSuccess;
public readonly TcpClient TcpClient;
public ConnectionState(TcpClient tcpClient)
{
this.TcpClient = tcpClient;
}
}
异常在 PingCallback(IAsyncResult result)
中的 catch
子句中被捕获,错误消息为 "No such host is known"。
当我运行netstat -an
时,可以看到我的本地服务器确实在监听31311端口:
如果我将 TcpClient tcpClient = new TcpClient();
更改为 TcpClient tcpClient = new TcpClient(LocalHost, Port);
,则会在那里抛出相同的异常(具有相同的错误消息)。
我该如何解决这个问题?
指定的主机名不正确。当你在没有异步的情况下尝试时,你应该有类似下面的调用。
TcpClient tcpClient = new TcpClient("127.0.0.1", 31311);
在异步连接中,您应该如下指定
tcpClient.BeginConnect(host: "127.0.0.1", ...)
这应该可以解决问题
我知道 Whosebug 上已经有几个问题询问这个特定的异常,但我还没有找到解决我的问题的答案。
这里是相关的代码片段:
public static class Server
{
public const string LocalHost = "http://127.0.0.1";
public const int Port = 31311;
public static readonly string FullAddress = $"{LocalHost}:{Port}";
private static readonly TimeSpan RetryConnectionInterval = TimeSpan.FromSeconds(10);
public static async Task AwaitStart()
{
try
{
TcpClient tcpClient = new TcpClient();
ConnectionState connectionState = new ConnectionState(tcpClient);
tcpClient.BeginConnect(
host: HostAddress,
port: Port,
requestCallback: PingCallback,
state: connectionState);
bool startedSuccessfully = connectionState.IsSuccess;
while (!startedSuccessfully)
{
await Task.Delay(RetryConnectionInterval);
startedSuccessfully = connectionState.IsSuccess;
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
private static void PingCallback(IAsyncResult result)
{
ConnectionState state = (ConnectionState)result.AsyncState;
try
{
state.TcpClient.EndConnect(result);
state.IsSuccess = true;
Console.WriteLine("The server is successfully started.");
}
catch (SocketException)
{
Console.WriteLine($"The server is not yet started. Re-attempting connection in {RetryConnectionInterval.Seconds} seconds.");
Wait(RetryConnectionInterval).GetAwaiter().GetResult();
state.TcpClient.BeginConnect(host: HostAddress, port: Port, requestCallback: PingCallback, state: state);
}
}
private static async Task Wait(TimeSpan duration)
{
await Task.Delay(duration);
}
}
public class ConnectionState
{
public bool IsSuccess;
public readonly TcpClient TcpClient;
public ConnectionState(TcpClient tcpClient)
{
this.TcpClient = tcpClient;
}
}
异常在 PingCallback(IAsyncResult result)
中的 catch
子句中被捕获,错误消息为 "No such host is known"。
当我运行netstat -an
时,可以看到我的本地服务器确实在监听31311端口:
如果我将 TcpClient tcpClient = new TcpClient();
更改为 TcpClient tcpClient = new TcpClient(LocalHost, Port);
,则会在那里抛出相同的异常(具有相同的错误消息)。
我该如何解决这个问题?
指定的主机名不正确。当你在没有异步的情况下尝试时,你应该有类似下面的调用。
TcpClient tcpClient = new TcpClient("127.0.0.1", 31311);
在异步连接中,您应该如下指定
tcpClient.BeginConnect(host: "127.0.0.1", ...)
这应该可以解决问题