检查服务器是否开启/关闭 [英] Checking whether the server is on/off

查看:483
本文介绍了检查服务器是否开启/关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一些程序,以便检测某个游戏服务器是否开启或关闭,我具有服务器的IP和端口.

I am trying to build some program that detects if a server of some game is on or off, I have the IP and the port of the server.

我该怎么做才能检测服务器是打开还是关闭?插座?平课?

What do I have to do to detect whether the server is on or off? Sockets? Ping class?

如果你们能给我一个代码示例之类的东西,那就太好了.

It would be nice if you guys could give me a code sample or something.

非常感谢!

推荐答案

请参见套接字.连接

public static void Connect1(string host, int port)
{
    IPAddress[] IPs = Dns.GetHostAddresses(host);

    Socket s = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream,
        ProtocolType.Tcp);

    Console.WriteLine("Establishing Connection to {0}", 
        host);
    s.Connect(IPs[0], port); // Catch any exceptions here.
    Console.WriteLine("Connection established");
}   

通过ping服务器,您可以指示服务器是"on"还是"off";它不会指示您尝试连接的服务是否正在运行.

By pinging a server you can get an indication whether the server is 'on' or 'off'; it doesn't give an indication whether the service you're trying to connect to is running or not.

如果第一种选择是您要寻找的(关于服务器是否已打开并已连接到Internet的一般想法),则ping操作即可满足此任务.

if the first option is what you're looking for (a general idea whether the server is on&connected to the internet), pinging will suffice for this task.

按照效率,由于服务器和客户端之间仅交换2个数据包,而不是建立约3个数据包的完整连接,因此ping效率更高(从大规模的角度来讲)- 请参阅TCP三向握手

As per the efficiency - pinging is more efficient (well, in large scale terms) as only 2 packets are being exchanged between the server and the client, rather than a full connection establishment which is about 3 packets - See TCP 3-Way handshake

这篇关于检查服务器是否开启/关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆