简体   繁体   中英

Determine Global IP in the same network vb.net,C#

My software saves information in three different ways depending on the type of database connection (1-Local / 2-Network / 3-Internet).

I can recognize the type of database connection depending on the server name from the connection string.

First problem which has been solved is when the customer type the global address instead of typing Localhost on the server machine.

I used this function to check if the address belongs to the same machine or not:

Public Function IsLocalIpAddress(host As String) As Boolean
    Try
        ' get host IP addresses
        Dim hostIPs As Net.IPAddress() = Net.Dns.GetHostAddresses(host)
        ' get local IP addresses
        Dim localIPs As Net.IPAddress() = Net.Dns.GetHostAddresses(Net.Dns.GetHostName())
        ' test if any host IP equals to any local IP or to localhost
        For Each hostIP As Net.IPAddress In hostIPs
            ' is localhost
            If Net.IPAddress.IsLoopback(hostIP) Then
                Return True
            End If
            ' is local address
            For Each localIP As Net.IPAddress In localIPs
                If hostIP.Equals(localIP) Then
                    Return True
                End If
            Next
        Next
    Catch
    End Try
    Return False
End Function

But when he types the global IP from another computer in the same network the software recognize the connection as (Internet), not (Network)

So I need something to tell me that this global ip is in the same network to make my software deals with this connection correctly.

I did some tricks maybe it solves my question temporary

1- First Trick

Ping the Global IP address , if the RoundtripTime less than 2 milliseconds it should be in the same network ( I tested most famous websites on my data line and they are all above 20 )

Dim ping1 As New System.Net.NetworkInformation.Ping()
            Try
                Dim PingReply1 As System.Net.NetworkInformation.PingReply = ping1.Send(GetFormCs(SrcMCS, CsDataEnum.Server))
                If PingReply1.Status = Net.NetworkInformation.IPStatus.Success Then
                    If PingReply1.RoundtripTime() <= 2 Then
                        dbHost = Hst.Network
                        ForcedToNetwork = True
                    End If
                End If
                PingReply1 = Nothing
            Catch ex As Exception
            End Try
            ping1 = Nothing

2- Second Trick

I Check The DSN IP for the connection ... if it equals to server Global IP it means that the connection is in the same network

Dim webCL As New System.Net.WebClient
    Dim PublicIP As String = ""
    Try
        Dim foo As New Xml.XmlDocument
        foo.LoadXml(System.Text.Encoding.ASCII.GetString((webCL.DownloadData("http://checkip.dyndns.org/"))))
        Dim curNode As Xml.XmlNode = foo.DocumentElement.FirstChild
        curNode = curNode.NextSibling
        response = curNode.InnerText.Remove(0, curNode.InnerText.IndexOf(":") + 1)
    Catch ex As Exception
        PublicIP = "ERROR"
    End Try
      If PublicIP = Database_Sever_IP Then
          If dbHost = Hst.Internet Then
              dbHost = Hst.Network
              ForcedToNetwork = True
          End If
      End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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