Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#
Article

HostName, IP, and MAC Address

Rate me:
Please Sign up or sign in to vote.
3.60/5 (32 votes)
23 Jan 2005 345.7K   21K   104   50
This will guide you to understand and trace network host information, IP, and MAC address information.

Sample Image - Host_Info_within_Network.jpg

Introduction

This article is used to get the intranet information in a network. It shows information like the host name, corresponding IP address and also physical address.

Using the code

The code retrieves all the System names using the Directory entries information.

C#
//       // Use Your work Group WinNT://Baba(Work Group Name)
//     DirectoryEntry DomainEntry = 
//         new DirectoryEntry("WinNT://" + this.TxtWorkGroup.Text.Trim());
//     DomainEntry.Children.SchemaFilter.Add("computer");

Then we collect the IP address using the DNS.

C#
//    System.Net.IPHostEntry Tempaddr = 
 //     (System.Net.IPHostEntry)Dns.GetHostByName(machine.Name);
//    System.Net.IPAddress[] TempAd = Tempaddr.AddressList;

The physical MAC address is collected using the DLL "iphlpapi.dll" with the API SendARP().

For each System, we use the Hostname to retrieve the MAC address.

C#
//    [DllImport("iphlpapi.dll", ExactSpelling=true)]
//    public static extern int SendARP( int DestIP, int SrcIP, 
//         [Out] byte[] pMacAddr, ref int PhyAddrLen );

Points of Interest

This is my first article in CodeProject.

History

  • 01/24/2005

    Article released.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Technical Lead
India India
I loves computers. I am interested in programming such like Networking using Sockets, Imaging, Telecom and robotics.

I am very much interested in programming in windows and Linux.

I am working with Verizon Data Services India Ltd.,

Comments and Discussions

 
GeneralRe: MAC address of Linux, Solaris... machine? Pin
MahendranG26-Dec-06 6:53
MahendranG26-Dec-06 6:53 
Hi Minh,

You clearly mentioned that, You are going to working in the local network, fine then. It should be in the Server side function. In Server side you can get the IP address of the Client which requesting web page (i know it is possible in Dotnet). For the IP address you can get the MAC using the Any Server Functions (if it is a windows server this functions also will work).

Regards,
Mahendran.G

Mahendran. G
Sr. Software Engg.
Satyam computer Services,
Chennai, India.

QuestionHow to get the MAC from NIC Hardware Pin
Reader Man San31-Oct-06 20:51
professionalReader Man San31-Oct-06 20:51 
GeneralHostname from IP Address Pin
DaLucifer30-Jun-06 23:03
DaLucifer30-Jun-06 23:03 
GeneralRe: Hostname from IP Address Pin
MahendranG25-Jul-06 17:57
MahendranG25-Jul-06 17:57 
GeneralRe: Hostname from IP Address Pin
DaLucifer26-Jul-06 5:07
DaLucifer26-Jul-06 5:07 
QuestionHow to Get MAC Address from an IP Address in Internet Pin
dathq22-May-06 7:02
dathq22-May-06 7:02 
AnswerRe: How to Get MAC Address from an IP Address in Internet Pin
darkhuntercode28-Jan-11 9:25
darkhuntercode28-Jan-11 9:25 
GeneralMAC Address of PC;s turned off... Pin
KevinReece19-Apr-06 4:34
KevinReece19-Apr-06 4:34 
GeneralRe: MAC Address of PC;s turned off... Pin
MahendranG19-Apr-06 18:48
MahendranG19-Apr-06 18:48 
GeneralVB.NET 2005 Example Pin
FloatingPointCode6-Apr-06 4:43
FloatingPointCode6-Apr-06 4:43 
GeneralRe: VB.NET 2005 Example Pin
MahendranG10-Apr-06 17:14
MahendranG10-Apr-06 17:14 
GeneralRe: VB.NET 2005 Example Pin
FloatingPointCode11-Apr-06 5:04
FloatingPointCode11-Apr-06 5:04 
GeneralRe: VB.NET 2005 Example Pin
juacar0129-Jul-06 9:20
juacar0129-Jul-06 9:20 
GeneralsendARP Pin
ESTAN27-Jan-06 4:50
ESTAN27-Jan-06 4:50 
Generalworkgroup name Pin
rixwan24-Jan-06 3:33
rixwan24-Jan-06 3:33 
GeneralRe: workgroup name Pin
ESTAN27-Jan-06 4:54
ESTAN27-Jan-06 4:54 
GeneralRe: workgroup name [modified] Pin
rchi83-Sep-07 21:04
rchi83-Sep-07 21:04 
GeneralRe: workgroup name Pin
FloatingPointCode6-Apr-06 4:39
FloatingPointCode6-Apr-06 4:39 
Generalwirless Pin
aglt11-Sep-05 2:24
aglt11-Sep-05 2:24 
GeneralRe: wirless Pin
Anonymous16-Sep-05 3:37
Anonymous16-Sep-05 3:37 
Generaldetermining type of connection Pin
shihab1234567824-Jul-05 6:35
shihab1234567824-Jul-05 6:35 
GeneralRe: determining type of connection Pin
Anonymous27-Jul-05 7:19
Anonymous27-Jul-05 7:19 
Generaldetect multiple lan cards Pin
Adnan Siddiqi17-Apr-05 20:48
Adnan Siddiqi17-Apr-05 20:48 
GeneralRe: detect multiple lan cards Pin
MahendranG17-Apr-05 22:49
MahendranG17-Apr-05 22:49 
GeneralRe: detect multiple lan cards Pin
Adnan Siddiqi17-Apr-05 23:47
Adnan Siddiqi17-Apr-05 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.