简体   繁体   English

如何使用 C# 检查 IP 地址是否可达

[英]How to check whether IP address is reachable or not using C#

I am validating whether the given IP is reachable or not.我正在验证给定的 IP 是否可以访问。 In server PING, TELNET, etc options are disabled.在服务器 PING、TELNET 等选项被禁用。 I tried with Socket Connection but this needs the given PORT to be allowed.我尝试使用 Socket Connection,但这需要允许给定的 PORT。

Is there any reliable solution to check this.有没有可靠的解决方案来检查这一点。 It should work on all the environment [independent of OS]它应该适用于所有环境[独立于操作系统]

Please guide me to implement this on C#请指导我在 C# 上实现这个

There is no such thing as "IP address is reachable" in general.一般来说,没有“IP地址可达”之类的东西。 Relevant is, if a specific service on a server can be successfully contacted - because at the end one wants to use the server for a specific purpose.相关的是,如果可以成功联系服务器上的特定服务 - 因为最终希望将服务器用于特定目的。 Checking if one can ping the server says nothing about this: it might be that ping is disabled or blocked but that the server can still be reached by HTTP (ie web).检查是否可以 ping 服务器对此一无所知:可能是 ping 被禁用或阻止,但 HTTP(即 web)仍然可以访问服务器。 Similar it might be that ping works but HTTP will not work either because there is no HTTP service running at the server or because some firewall in between is blocking access to HTTP for all or even only for specific clients.类似地,ping 可能有效,但 HTTP 将不起作用,因为服务器上没有运行 HTTP 服务,或者因为中间的某些防火墙阻止了对 HTTP 的访问,甚至只针对所有特定客户端。

Thus, you have to decide what you really want to know, ie what you want to achieve with the knowledge of the IP being reachable.因此,您必须决定您真正想知道什么,即您想通过了解 IP 可达到的知识来实现什么。 For example if you want to use this knowledge to access the web server there later you should try a connection at port 80 (default port for HTTP).例如,如果您想稍后使用此知识访问 web 服务器,您应该尝试在端口 80(HTTP 的默认端口)上进行连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM