c#检查端口是否正在积极侦听?
我使用以下代码实现此目标:c#检查端口是否正在积极侦听?,c#,sockets,C#,Sockets,我使用以下代码实现此目标: public static bool IsServerListening() { var endpoint = new IPEndPoint(IPAddress.Parse("201.212.1.167"), 2593); var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); t
public static bool IsServerListening()
{
var endpoint = new IPEndPoint(IPAddress.Parse("201.212.1.167"), 2593);
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Connect(endpoint, TimeSpan.FromSeconds(5));
return true;
}
catch (SocketException exception)
{
if (exception.SocketErrorCode == SocketError.TimedOut)
{
Logging.Log.Warn("Timeout while connecting to UO server game port.", exception);
}
else
{
Logging.Log.Error("Exception while connecting to UO server game port.", exception);
}
return false;
}
catch (Exception exception)
{
Logging.Log.Error("Exception while connecting to UO server game port.", exception);
return false;
}
finally
{
socket.Close();
}
}
下面是我对Socket
类的扩展方法:
public static class SocketExtensions
{
public const int CONNECTION_TIMEOUT_ERROR = 10060;
/// <summary>
/// Connects the specified socket.
/// </summary>
/// <param name="socket">The socket.</param>
/// <param name="endpoint">The IP endpoint.</param>
/// <param name="timeout">The connection timeout interval.</param>
public static void Connect(this Socket socket, EndPoint endpoint, TimeSpan timeout)
{
var result = socket.BeginConnect(endpoint, null, null);
bool success = result.AsyncWaitHandle.WaitOne(timeout, true);
if (!success)
{
socket.Close();
throw new SocketException(CONNECTION_TIMEOUT_ERROR); // Connection timed out.
}
}
}
公共静态类SocketExtensions
{
公共常量int连接超时错误=10060;
///
///连接指定的套接字。
///
///插座。
///IP端点。
///连接超时时间间隔。
公共静态void Connect(此套接字、端点、TimeSpan超时)
{
var result=socket.BeginConnect(端点,null,null);
bool success=result.AsyncWaitHandle.WaitOne(超时,true);
如果(!成功)
{
socket.Close();
抛出新的SocketException(连接超时错误);//连接超时。
}
}
}
问题是这段代码在我的开发环境中工作,但当我将其移动到生产环境中时,它总是超时(无论我将超时间隔设置为5秒还是20秒)
是否有其他方法可以检查IP是否在该特定端口积极侦听
我无法在宿主环境中执行此操作的原因是什么?您可以从命令行运行
netstat-na
以查看所有(包括侦听)端口
如果添加-b
,还将看到链接到每个连接/侦听的可执行文件
在.NET中,您可以使用获取所有侦听连接。您可以使用以下代码进行检查:
TcpClient tc = new TcpClient();
try
{
tc.Connect(<server ipaddress>, <port number>);
bool stat = tc.Connected;
if (stat)
MessageBox.Show("Connectivity to server available.");
tc.Close();
}
catch(Exception ex)
{
MessageBox.Show("Not able to connect : " + ex.Message);
tc.Close();
}
TcpClient tc=newtcpclient();
尝试
{
tc.Connect(,);
bool stat=tc.已连接;
如果(统计)
Show(“与服务器的连接可用”);
tc.Close();
}
捕获(例外情况除外)
{
MessageBox.Show(“无法连接:+ex.Message”);
tc.Close();
}
在生产网络中处于活动状态的防火墙可以很好地解释这个问题。您在公共IP上使用了一个不寻常的端口号。为什么要使用精确的IP?一般来说,最好的解决方案是侦听所有IP(如果可以接受的话)。因为我想检查该IP是否在该端口上侦听?我已将答案-.NET解决方案更新为netstat-na
是GetActiveTcpListeners
和getactivetcpcconnections
上的IPGlobalProperties
。但我希望侦听器位于该特定IP上,不是本地IPMy答案显示了如何检查侦听端口-您应该在服务器端进行检查,您的示例代码是客户端。正如@Hans提到的,防火墙通常会阻止不允许的连接。这是毫无意义的,我想检查服务器是否在线并侦听该端口,据我所知,它可能会被关闭,并且有一个网站应该报告服务器的状态。从连接角度看,您的客户端检查代码是正常的。您是否可以手动检查服务器是否正在侦听,防火墙是否正确配置,以及您是否可以使用telnet或其他工具从本地控制台连接到指定的TPC端口。