Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 确定服务器是否正在侦听给定端口_C#_.net_Networking_Sockets_Tcp - Fatal编程技术网

C# 确定服务器是否正在侦听给定端口

C# 确定服务器是否正在侦听给定端口,c#,.net,networking,sockets,tcp,C#,.net,Networking,Sockets,Tcp,我需要轮询正在运行某些专用软件的服务器,以确定此服务是否正在运行。使用wireshark,我已经能够缩小其使用的TCP端口的范围,但似乎流量是加密的 在我的例子中,如果服务器正在接受连接(即telnet serverName 1234),那么服务将启动,一切正常。换句话说,我不需要做任何实际的数据交换,只需要打开一个连接,然后安全地关闭它 我想知道如何用C#和套接字来模拟这一点。我的网络编程基本上以WebClient结束,因此非常感谢这里的任何帮助。使用TcpClient类连接服务器。只需使用T

我需要轮询正在运行某些专用软件的服务器,以确定此服务是否正在运行。使用wireshark,我已经能够缩小其使用的TCP端口的范围,但似乎流量是加密的

在我的例子中,如果服务器正在接受连接(即telnet serverName 1234),那么服务将启动,一切正常。换句话说,我不需要做任何实际的数据交换,只需要打开一个连接,然后安全地关闭它


我想知道如何用C#和套接字来模拟这一点。我的网络编程基本上以WebClient结束,因此非常感谢这里的任何帮助。

使用TcpClient类连接服务器。

只需使用
TcpClient
尝试连接到服务器,TcpClient。如果连接失败,connect将引发异常

bool IsListening(string server, int port)
{
    using(TcpClient client = new TcpClient())
    {
        try
        {
            client.Connect(server, port);
        }
        catch(SocketException)
        {
            return false;
        }
        client.Close();
        return true;
    }
}

这个过程其实很简单

using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
    try
    {
        socket.Connect(host, port);
    }
    catch (SocketException ex)
    {
        if (ex.SocketErrorCode == SocketError.ConnectionRefused) 
        {
            // ...
        }
    }
}

我使用了以下代码。有一个警告。。。在高事务性环境中,客户端的可用端口可能会耗尽,因为操作系统释放套接字的速率与.NET代码释放套接字的速率不同

如果有人有更好的想法,请发帖。我看到服务器无法再进行传出连接时出现了滚雪球问题。我正在研究一个更好的解决方案

public static bool IsServerUp(string server, int port, int timeout)
    {
        bool isUp;

        try
        {
            using (TcpClient tcp = new TcpClient())
            {
                IAsyncResult ar = tcp.BeginConnect(server, port, null, null);
                WaitHandle wh = ar.AsyncWaitHandle;

                try
                {
                    if (!wh.WaitOne(TimeSpan.FromMilliseconds(timeout), false))
                    {
                        tcp.EndConnect(ar);
                        tcp.Close();
                        throw new SocketException();
                    }

                    isUp = true;
                    tcp.EndConnect(ar);
                }
                finally
                {
                    wh.Close();
                }
            } 
        }
        catch (SocketException e)
        {
            LOGGER.Warn(string.Format("TCP connection to server {0} failed.", server), e);
            isUp = false;
        }

        return isUp;

有没有办法调整连接超时?它似乎失败了,但只是在大约一分钟后…@Nate我相信这就是过程所需的时间。没有连接超时选项。我添加了
如果(ex.SocketErrorCode==SocketError.ConnectionRejected | | ex.SocketErrorCode==SocketError.TimedOut)
虽然看起来很简单,但我对服务器的性能下降有强烈的怀疑。假设它有一个新客户机的处理程序,每当客户机执行这样的检查时,它都会被额外触发。9年后的C#9难道没有更优雅的方式吗?理想情况下是基于事件的(很容易为此创建事件包装器,但同样会对服务器产生影响),有没有方法调整连接超时?它似乎失败了,但只是在大约一分钟后。。。