如何确定文件共享服务器是否处于联机状态? [英] How to determine whether file share server is online?

查看:350
本文介绍了如何确定文件共享服务器是否处于联机状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我要检查服务器的可用性,但我不知道它的股票,我可以将它的UNC名称(或IP地址)到Windows资源管理器窗口,即使无服务器共享目录

If I want to check server availability but I don't know about its shares, I can type its UNC name (or IP address) into Windows Explorer window even without server share directory:

如何使用这种编程方式在.NET来验证服务器是否处于联机状态?我想

My.Computer.FileSystem.DirectoryExists(UNCPath)

但这仅适用于UNC路径,比如 \\ MY_SERVER \用户,它不inteded与 \\ MY_SERVER

but this works only for UNC paths like \\MY_SERVER\Users, it is not inteded to use with \\MY_SERVER.

我想为避免平,并且坚持使用功能,这工作时,文件系统的工作原理,因为某些服务器可以有ICMP_ECHO禁用由于安全方面的原因。

I would like to avoid Ping and stick with functionality which works when file system works because some servers can have ICMP_ECHO disabled due to security reasons.

(在情况下,如果你想发布一些code,随意选择VB或C#。)

(In case if you wish to post some code, feel free to choose VB or C#.)

推荐答案

编辑: 我收到通知,这可能是重复的解决方案,以 Q / A别处提及。所不同的是,其他问题的接受其中的回答并没有真正的工作作为我,我有理由认为,使用WMI是限制的方法在此情况下(这需要凭据, 见下文)。在这样的回答,我给了它确实对我的作品的方式。 但是,如果你真的认为这是重复的,继续将其标记为结束,我会删除整个问题是不必要的。

I'm notified this may be duplicate solution to Q/A mentioned elsewhere. The difference is that the other question accepts the answer which did not really work for me and I have reasons to think that using WMI is limiting approach in this scenario (it requires credentials, see below). In this answer, I'm giving an approach which really works for me. But if you really think it is duplicate, continue marking it for closing and I'll remove the entire question as unnecessary.

我不断学习和尝试,最后我使用 TCP端口139 结算,这是的使用的文件共享机制。请看下面的源为草稿(不要忘了处理异常等),但最后我能有一张code这似乎为我工作:

I've continued studying and trying and finally I settled with using TCP port 139, the same which is used by file sharing mechanism. Consider the following source as a draft (and do not forget to handle exceptions etc.), but finally I can have piece of code which seems to work for me:

Public Shared Function IsFileShareServerOnline(PCName As String,
                                               Timeout as Integer) As Boolean

    Const Port As Integer = 139 'port for file sharing service

    Dim IsOnline As Boolean = False
    Dim IP As Net.IPAddress = Net.Dns.GetHostAddresses(PCName)(0)
    Dim Address As Net.IPAddress = Net.IPAddress.Parse(IP.ToString())
    Dim Socket As Net.Sockets.TcpClient = New Net.Sockets.TcpClient(Address.AddressFamily)
    Dim Connect = Socket.BeginConnect(Address, Port, Nothing, Nothing)
    Dim WaitingResult = Connect.AsyncWaitHandle.WaitOne(Timeout, False)

    If (Connect.IsCompleted) Then
        If WaitingResult Then
            Socket.EndConnect(Connect)
            IsOnline = True
        End If
    End If
    Socket.Close()
    Return IsOnline

End Function

在code具有在第一次运行它甚至不能如能提供检测远程计算机的问题。一旦重试它的工作原理。问题更新(一个错误的检测),只要在连接后较长时间尝试。我不是TCP / IP专家理解的原因,也许是我使用过短超时(1秒)。一种解决方法是给至少2连接尝试。

The code has an issue that on first run it cannot detect remote computer even if available. Upon retry it works. The problem renews (one bad detection) whenever the connection is attempted after longer time. I'm not TCP/IP expert to understand the cause, maybe I use too short timeout (1 sec). A workaround is to give at least 2 connection attempts.

在尝试这种做法,我也实现和测试基于WMI的查询,但他们并没有没有在远程计算机上预先进行身份验证什么是pretty的在我的情况下无用功。

Before trying this approach, I've also implemented and tested WMI-based queries, but they did not work without prior authentication at remote computer what was pretty useless in my scenario.

这篇关于如何确定文件共享服务器是否处于联机状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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