Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用C语言检查网络状态#_C#_.net_Network Programming - Fatal编程技术网

C# 用C语言检查网络状态#

C# 用C语言检查网络状态#,c#,.net,network-programming,C#,.net,Network Programming,如何检查我是否有开放的网络连接,以及是否可以联系c#中的特定ip地址?我在VB.Net中看到过一个示例,但它们都使用“我的”结构。 谢谢。好吧,您可以尝试连接到特定的ip,并处理拒绝和超时 查看System.Net.Sockets命名空间中的TcpClient类。第一个建议(IP连接) 您可以尝试使用以下方式连接到IP地址: IPEndPoint ipep = new IPEndPoint(Ipaddress.Parse("IP TO CHECK"), YOUR_PORT_I

如何检查我是否有开放的网络连接,以及是否可以联系c#中的特定ip地址?我在VB.Net中看到过一个示例,但它们都使用“我的”结构。
谢谢。

好吧,您可以尝试连接到特定的ip,并处理拒绝和超时

查看System.Net.Sockets命名空间中的TcpClient类。

第一个建议(IP连接) 您可以尝试使用以下方式连接到IP地址:

IPEndPoint ipep = new IPEndPoint(Ipaddress.Parse("IP TO CHECK"), YOUR_PORT_INTEGER);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
server.Connect(ipep);
我建议您检查“聊天”程序的代码。这些程序操纵大量的IP连接,并将使您了解如何检查IP是否可用

第二项建议(平) 你可以试试打乒乓球。这是一个好主意。您只需执行以下操作:

Ping netMon = new Ping();
PingResponse response = netMon.PingHost(hostname, 4);
if (response != null)
{
    ProcessResponse(response);
}

如果您对HTTP状态代码感兴趣,以下代码可以正常工作:

using System;
using System.Net;

class Program {

    static void Main () {
        HttpWebRequest req = WebRequest.Create(
            "http://www.oberon.ch/") as HttpWebRequest;
        HttpWebResponse rsp;
        try {
            rsp = req.GetResponse() as HttpWebResponse;
        } catch (WebException e) {
            if (e.Response is HttpWebResponse) {
                rsp = e.Response as HttpWebResponse;
            } else {
                rsp = null;
            }
        }
        if (rsp != null) {
            Console.WriteLine(rsp.StatusCode);
        }
    }

}

如果您只想检查网络是否正常,请使用:

bool networkUp
    = System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable();
要检查特定界面的状态(或其他信息),请使用:


要检查远程计算机的状态,则必须连接到该计算机(请参阅其他答案)

如果要监视状态的变化,请使用事件:


我的想法是使用一个静态类/模块来监视spereate线程。一个简单的DNS解析将确保您的网络是否正常运行。比斯平

Imports System.Net

Public Module Network_Monitor

Private InsideWorkNet As Boolean = vbFalse
Private Online_Status As Boolean = vbFalse
Private CurrentWorkIPAddress As New IPHostEntry
Private WithEvents Timer_Online_Check As New Timers.Timer With {.Interval = 5000, .Enabled = True, .AutoReset = True}


Public ReadOnly Property GetOnlineStatus() As String

    Get
        Return Online_Status
    End Get

End Property


Public Sub Initialize()

    Set_Online_Status()
    Timer_Online_Check.Start()

End Sub


Public Sub Set_Online_Status()

    If My.Computer.Network.IsAvailable Then
        Try
            Dim DNSTest As IPHostEntry = Dns.GetHostEntry("google.com")
            If DNSTest.AddressList.Length > 0 Then
                Online_Status = True
            Else : Online_Status = False

            End If

        Catch ex As System.Net.Sockets.SocketException

            Online_Status = False

        End Try
    End If

End Sub


Private Sub Timer_Online_Check_Elaspsed(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs) Handles Timer_Online_Check.Elapsed
    Set_Online_Status()
End Sub

Public Sub Detect_Work_Network()

    If Online_Status = True Then

        Dim WorkIP As IPHostEntry = New IPHostEntry()

        Try
            WorkIP = Dns.GetHostEntry("serverA.myworkdomain.local")
            If WorkIP.AddressList.Length > 0 Then

                InsideWorkNet = True
                CurrentWorkIPAddress = WorkIP
                'MessageBox.Show(WorkIP.HostName.ToString(), "WorkIP", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Sockets.SocketException

            Try
                WorkIP = Dns.GetHostEntry("serverA.myworkdomain.com")
                If WorkIP.AddressList.Length > 0 Then

                    InsideWorkNet = False
                    CurrentWorkIPAddress = WorkIP
                    ' MessageBox.Show(WorkIP.HostName.ToString(), "WorkIP", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Catch ey As Sockets.SocketException

            End Try
        End Try
    End If

End Sub

Public Function GetWorkServerName() As String
    If InsideWorkNet = True Then
        Return "serverA.myworkdomain.local"
    Else : Return "serverA.myworkdomain.com"
    End If


End Function



End Module

我还必须确定自己是在工作网络之内还是之外。防火墙两侧的不同服务器供应用程序通话。

您可以使用

if(System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
  //Do your stuffs when network available

}
else
{
 //Do stuffs when network not available

}

谢谢你的建议。ping类非常有用。ping数据包(ICMP回显请求)通常会被防火墙阻止,因此在这种情况下,使用这种方法测试防火墙另一端服务器的可用性将失败。
Imports System.Net

Public Module Network_Monitor

Private InsideWorkNet As Boolean = vbFalse
Private Online_Status As Boolean = vbFalse
Private CurrentWorkIPAddress As New IPHostEntry
Private WithEvents Timer_Online_Check As New Timers.Timer With {.Interval = 5000, .Enabled = True, .AutoReset = True}


Public ReadOnly Property GetOnlineStatus() As String

    Get
        Return Online_Status
    End Get

End Property


Public Sub Initialize()

    Set_Online_Status()
    Timer_Online_Check.Start()

End Sub


Public Sub Set_Online_Status()

    If My.Computer.Network.IsAvailable Then
        Try
            Dim DNSTest As IPHostEntry = Dns.GetHostEntry("google.com")
            If DNSTest.AddressList.Length > 0 Then
                Online_Status = True
            Else : Online_Status = False

            End If

        Catch ex As System.Net.Sockets.SocketException

            Online_Status = False

        End Try
    End If

End Sub


Private Sub Timer_Online_Check_Elaspsed(ByVal sender As Object, ByVal e As Timers.ElapsedEventArgs) Handles Timer_Online_Check.Elapsed
    Set_Online_Status()
End Sub

Public Sub Detect_Work_Network()

    If Online_Status = True Then

        Dim WorkIP As IPHostEntry = New IPHostEntry()

        Try
            WorkIP = Dns.GetHostEntry("serverA.myworkdomain.local")
            If WorkIP.AddressList.Length > 0 Then

                InsideWorkNet = True
                CurrentWorkIPAddress = WorkIP
                'MessageBox.Show(WorkIP.HostName.ToString(), "WorkIP", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Catch ex As Sockets.SocketException

            Try
                WorkIP = Dns.GetHostEntry("serverA.myworkdomain.com")
                If WorkIP.AddressList.Length > 0 Then

                    InsideWorkNet = False
                    CurrentWorkIPAddress = WorkIP
                    ' MessageBox.Show(WorkIP.HostName.ToString(), "WorkIP", MessageBoxButtons.OK, MessageBoxIcon.Information)
                End If
            Catch ey As Sockets.SocketException

            End Try
        End Try
    End If

End Sub

Public Function GetWorkServerName() As String
    If InsideWorkNet = True Then
        Return "serverA.myworkdomain.local"
    Else : Return "serverA.myworkdomain.com"
    End If


End Function



End Module
if(System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
  //Do your stuffs when network available

}
else
{
 //Do stuffs when network not available

}