检查服务器是否可用

时间:2020-12-04 16:55:02

I'm looking for a way to check if a server is still available. We have a offline application that saves data on the server, but if the serverconnection drops (it happens occasionally), we have to save the data to a local database instead of the online database. So we need a continues check to see if the server is still available.

我正在寻找一种方法来检查服务器是否仍然可用。我们有一个离线应用程序,可以在服务器上保存数据,但如果服务器连接断开(偶尔会发生),我们必须将数据保存到本地数据库而不是在线数据库。所以我们需要继续检查服务器是否仍然可用。

We are using C# for this application

我们在这个应用程序中使用C#

The check on the sqlconnection.open is not really an option because this takes about 20 sec before an error is thrown, we can't wait this long + I'm using some http services as well.

对sqlconnection.open的检查实际上不是一个选项,因为在抛出错误之前需要大约20秒,我们不能等待这么长时间+我也在使用一些http服务。

6 个解决方案

#1


Just use the System.Net.NetworkInformation.Ping class. If you server does not respond to ping (for some reason you decided to block ICMP Echo request) you'll have to invent you own service for this. Personally, I'm all for not blocking ICMP Echo requests, and I think this is the way to go. The ping command has been used for ages to check reachability of hosts.

只需使用System.Net.NetworkInformation.Ping类。如果您的服务器没有响应ping(由于某种原因您决定阻止ICMP Echo请求),您将必须为此创建自己的服务。就个人而言,我完全没有阻止ICMP Echo请求,我认为这是要走的路。 ping命令已经使用了很长时间来检查主机的可达性。

using System.Net.NetworkInformation;
var ping = new Ping();
var reply = ping.Send("google.com", 60 * 1000); // 1 minute time out (in ms)
// or...
reply = ping.Send(new IPAddress(new byte[]{127,0,0,1}), 3000);

#2


If the connection is as unreliable as you say, I would not use a seperate check, but make saving the data local part of the exception handling. I mean if the connection fails and throws an exception, you switch strategies and save the data locally.

如果连接与你说的一样不可靠,我就不会使用单独的检查,而是将数据保存在异常处理的本地部分。我的意思是如果连接失败并抛出异常,您切换策略并在本地保存数据。

If you check first and the connection drops afterwards (when you actually save data), then you still would still run into an exception you need to handle. So the initial check was unnecessary. The check would only be useful if you can assume that after a succesfull check the connection is up and stays up.

如果先检查并且之后连接丢失(当您实际保存数据时),那么您仍然会遇到需要处理的异常。所以初步检查是不必要的。如果您可以假设在成功检查后连接已启动并保持正常,则此检查将非常有用。

#3


From your question it appears the purpose of connecting to the server is to use its database. Your priority must be to check whether you can successfully connect to the database. It doesn't matter if you can PING the server or get an HTTP response (as suggested in other answers), your process will fail unless you successfully establish a connection to the database. You mention that checking a database connection takes too long, why don't you just change the Connection Timeout setting in your application's connection string to a more impatient value such as 5 seconds (Connection Timeout=5)?

从您的问题看来,连接到服务器的目的是使用其数据库。您的首要任务是检查是否可以成功连接到数据库。如果您可以PING服务器或获得HTTP响应(如其他答案中所建议的那样)并不重要,除非您成功建立与数据库的连接,否则您的进程将失败。您提到检查数据库连接时间过长,为什么不将应用程序连接字符串中的“连接超时”设置更改为更加不耐烦的值(例如5秒(连接超时= 5))?

#4


If this is an sql server then you can just try to open a new connection to it. If the SqlConnection.Open method fails then you can check the error message to determine if the server is unavailable.

如果这是一个SQL Server,那么你可以尝试打开一个新的连接。如果SqlConnection.Open方法失败,则可以检查错误消息以确定服务器是否不可用。

#5


What you are doing now is:

你现在在做的是:

  • use distant server
  • 使用远程服务器

  • if distant server fails, resort to local cache
  • 如果远程服务器出现故障,请使用本地缓存

How to determine if the server is available? Use a catch block. That's the simplest to code.

如何确定服务器是否可用?使用catch块。这是最简单的代码。


If you actually have a local database (and not, for example, a list of transactions or data waiting to be inserted), I would turn the design around:

如果你实际上有一个本地数据库(而不是,例如,等待插入的事务或数据列表),我会改变设计:

  • use the local database
  • 使用本地数据库

  • regularly synchronize the local database and the distant database
  • 定期同步本地数据库和远程数据库


I'll let you be the judge on concurrency constraints and other stuff related to your application to pick a solution.

我将让您成为并发约束和与您的应用程序相关的其他东西的判断来选择解决方案。

#6


Since you want to see if the database server is there either catch any errors when you attempt to connect to the database or use a socket and attempt a raw connection to the server on some service, I'd suggest the database as that is the resource you need.

既然您想要查看数据库服务器是否存在当您尝试连接到数据库或使用套接字并在某些服务上尝试与服务器的原始连接时捕获任何错误,我建议数据库,因为这是资源你需要。

#1


Just use the System.Net.NetworkInformation.Ping class. If you server does not respond to ping (for some reason you decided to block ICMP Echo request) you'll have to invent you own service for this. Personally, I'm all for not blocking ICMP Echo requests, and I think this is the way to go. The ping command has been used for ages to check reachability of hosts.

只需使用System.Net.NetworkInformation.Ping类。如果您的服务器没有响应ping(由于某种原因您决定阻止ICMP Echo请求),您将必须为此创建自己的服务。就个人而言,我完全没有阻止ICMP Echo请求,我认为这是要走的路。 ping命令已经使用了很长时间来检查主机的可达性。

using System.Net.NetworkInformation;
var ping = new Ping();
var reply = ping.Send("google.com", 60 * 1000); // 1 minute time out (in ms)
// or...
reply = ping.Send(new IPAddress(new byte[]{127,0,0,1}), 3000);

#2


If the connection is as unreliable as you say, I would not use a seperate check, but make saving the data local part of the exception handling. I mean if the connection fails and throws an exception, you switch strategies and save the data locally.

如果连接与你说的一样不可靠,我就不会使用单独的检查,而是将数据保存在异常处理的本地部分。我的意思是如果连接失败并抛出异常,您切换策略并在本地保存数据。

If you check first and the connection drops afterwards (when you actually save data), then you still would still run into an exception you need to handle. So the initial check was unnecessary. The check would only be useful if you can assume that after a succesfull check the connection is up and stays up.

如果先检查并且之后连接丢失(当您实际保存数据时),那么您仍然会遇到需要处理的异常。所以初步检查是不必要的。如果您可以假设在成功检查后连接已启动并保持正常,则此检查将非常有用。

#3


From your question it appears the purpose of connecting to the server is to use its database. Your priority must be to check whether you can successfully connect to the database. It doesn't matter if you can PING the server or get an HTTP response (as suggested in other answers), your process will fail unless you successfully establish a connection to the database. You mention that checking a database connection takes too long, why don't you just change the Connection Timeout setting in your application's connection string to a more impatient value such as 5 seconds (Connection Timeout=5)?

从您的问题看来,连接到服务器的目的是使用其数据库。您的首要任务是检查是否可以成功连接到数据库。如果您可以PING服务器或获得HTTP响应(如其他答案中所建议的那样)并不重要,除非您成功建立与数据库的连接,否则您的进程将失败。您提到检查数据库连接时间过长,为什么不将应用程序连接字符串中的“连接超时”设置更改为更加不耐烦的值(例如5秒(连接超时= 5))?

#4


If this is an sql server then you can just try to open a new connection to it. If the SqlConnection.Open method fails then you can check the error message to determine if the server is unavailable.

如果这是一个SQL Server,那么你可以尝试打开一个新的连接。如果SqlConnection.Open方法失败,则可以检查错误消息以确定服务器是否不可用。

#5


What you are doing now is:

你现在在做的是:

  • use distant server
  • 使用远程服务器

  • if distant server fails, resort to local cache
  • 如果远程服务器出现故障,请使用本地缓存

How to determine if the server is available? Use a catch block. That's the simplest to code.

如何确定服务器是否可用?使用catch块。这是最简单的代码。


If you actually have a local database (and not, for example, a list of transactions or data waiting to be inserted), I would turn the design around:

如果你实际上有一个本地数据库(而不是,例如,等待插入的事务或数据列表),我会改变设计:

  • use the local database
  • 使用本地数据库

  • regularly synchronize the local database and the distant database
  • 定期同步本地数据库和远程数据库


I'll let you be the judge on concurrency constraints and other stuff related to your application to pick a solution.

我将让您成为并发约束和与您的应用程序相关的其他东西的判断来选择解决方案。

#6


Since you want to see if the database server is there either catch any errors when you attempt to connect to the database or use a socket and attempt a raw connection to the server on some service, I'd suggest the database as that is the resource you need.

既然您想要查看数据库服务器是否存在当您尝试连接到数据库或使用套接字并在某些服务上尝试与服务器的原始连接时捕获任何错误,我建议数据库,因为这是资源你需要。