C# 检查服务器是否可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/614336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a server is available
提问by user29964
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 服务。
采纳答案by John Leidegren
Just use the System.Net.NetworkInformation.Pingclass. If your server does not respond to ping (for some reason you decided to block ICMP Echo request) you'll have to invent your 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);
回答by UnkwnTech
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.
由于您想查看数据库服务器是否在您尝试连接到数据库时捕获任何错误或使用套接字并尝试在某些服务上与服务器建立原始连接,因此我建议使用数据库,因为这是资源你需要。
回答by Rune Grimstad
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 方法失败,那么您可以检查错误消息以确定服务器是否不可用。
回答by f3lix
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.
如果您先检查然后连接断开(当您实际保存数据时),那么您仍然会遇到需要处理的异常。所以最初的检查是不必要的。仅当您可以假设在成功检查后连接已启动并保持连接时,该检查才有用。
回答by DonkeyMaster
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.
我会让你判断并发约束和其他与你的应用程序相关的东西来选择一个解决方案。
回答by Chris Driver
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
响应(如其他答案中所建议的那样),除非您成功建立到数据库的连接,否则您的过程将失败。您提到检查数据库连接花费的时间太长,为什么不Connection Timeout
将应用程序连接字符串中的设置更改为更不耐烦的值,例如 5 秒 ( Connection Timeout=5
)?