什么是我的互联网访问IP [英] What is my internet accessing IP

查看:229
本文介绍了什么是我的互联网访问IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装在我的电脑两个局域网卡。一个是互联网连接,另一个用于共享上网客户端计算机。我得到我的IP与此code:

I have two lan cards installed in my pc. One is for internet connection and another for share the internet to client machines. I am getting my IP with this code:

IPHostEntry HosyEntry = Dns.GetHostEntry((Dns.GetHostName()));
foreach (IPAddress ip in HosyEntry.AddressList)
{
    trackingIp = ip.ToString();
    textBox1.Text += trackingIp + ",";
}

我如何才能找到其中一个我的互联网连接的IP(我不想用文字处理这样做)?

How can I find which one my internet connecting IP (I dont want to do it by text processing)?

推荐答案

确定。我写了2种方法。

Ok. I wrote 2 methods.

第一种方法是快,但需要使用一个插座。 它试图利用每一个本地IP连接到远程主机。

First method is faster but require to use a socket. It tries to connect to remote host using each local IP.

二methos慢,没有使用插座。它连接到远程主机(得到响应,浪费一些流量)和查找本地IP在活动连接表。

Second methos is slower and did not use sockets. It connects to remote host (get response, waste some traffic) and look for local IP in the active connections table.

code是草案,以便仔细检查它。

Code is draft so double check it.

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Net.NetworkInformation;
    using System.Net.Sockets;

    class Program
    {
        public static List<IPAddress> GetInternetIPAddressUsingSocket(string internetHostName, int port)
        {
            // get remote host  IP. Will throw SocketExeption on invalid hosts
            IPHostEntry remoteHostEntry = Dns.GetHostEntry(internetHostName);
            IPEndPoint remoteEndpoint = new IPEndPoint(remoteHostEntry.AddressList[0], port);

            // Get all locals IP
            IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());

            var internetIPs = new List<IPAddress>();
            // try to connect using each IP             
            foreach (IPAddress ip in hostEntry.AddressList) {
                IPEndPoint localEndpoint = new IPEndPoint(ip, 80);

                bool endpointIsOK = true;
                try {
                    using (Socket socket = new Socket(localEndpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) {
                        socket.Connect(remoteEndpoint);
                    }
                }
                catch (Exception) {
                    endpointIsOK = false;
                }

                if (endpointIsOK) {
                    internetIPs.Add(ip); // or you can return first IP that was found as single result.
                }
            }

            return internetIPs;
        }

        public static HashSet <IPAddress> GetInternetIPAddress(string internetHostName)
        {
            // get remote IPs
            IPHostEntry remoteMachineHostEntry = Dns.GetHostEntry(internetHostName);

            var internetIPs = new HashSet<IPAddress>();

            // connect and search for local IP
            WebRequest request = HttpWebRequest.Create("http://" + internetHostName);
            using (WebResponse response = request.GetResponse()) {
                IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
                TcpConnectionInformation[] connections = properties.GetActiveTcpConnections();
                response.Close();

                foreach (TcpConnectionInformation t in connections) {
                    if (t.State != TcpState.Established) {
                        continue;
                    }

                    bool isEndpointFound = false;
                    foreach (IPAddress ip in remoteMachineHostEntry.AddressList) {
                        if (ip.Equals(t.RemoteEndPoint.Address)) {
                            isEndpointFound = true;
                            break;
                        }
                    }

                    if (isEndpointFound) {
                        internetIPs.Add(t.LocalEndPoint.Address);
                    }
                }
            }

            return internetIPs;
        }


        static void Main(string[] args)
        {

            List<IPAddress> internetIP = GetInternetIPAddressUsingSocket("google.com", 80);
            foreach (IPAddress ip in internetIP) {
                Console.WriteLine(ip);
            }

            Console.WriteLine("======");

            HashSet<IPAddress> internetIP2 = GetInternetIPAddress("google.com");
            foreach (IPAddress ip in internetIP2) {
                Console.WriteLine(ip);
            }

            Console.WriteLine("Press any key");
            Console.ReadKey(true);
        }
    }
}

这篇关于什么是我的互联网访问IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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