Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1066685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:03:14+00:00 2026-05-16T20:03:14+00:00

I have two lan cards installed in my pc. One is for internet connection

  • 0

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 + ",";
}

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:03:14+00:00Added an answer on May 16, 2026 at 8:03 pm

    Ok. I wrote 2 methods.

    First method is faster but require to use a socket.
    It tries to connect to remote host using each local 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 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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine a situation, I have PC with two lan cards, one is connected to
I have a PPPOE connection on a computer. That computer has two LAN cards
Problem: I have two spreadsheets that each serve different purposes but contain one particular
I have two spreadsheets... when one gets modified in a certain way I want
Background information Let's say I have two database servers, both SQL Server 2008. One
Let's say I have two applications which have to work together to a certain
I am willing to build a prototype of network appliance. This appliance is suppose
Problem Users from other IPs on the (Windows XP) LAN suddenly cannot connect to
I'm using jQuery. I have a string with a block of special characters (begin
So I'm making a app for a bank, but it doesnt manage very important

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.