C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 307,104 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,042 people online right now. Registration is fast and FREE... Join Now!




Host Name To IP Updated

 
Reply to this topicStart new topic

> Host Name To IP Updated, Get IP From Hostname

noorahmad
Group Icon



post 12 Apr, 2009 - 03:23 AM
Post #1


biggrin.gif
A Small Application Hostname To IP in C#
IPB Image

In this tutorial I will be showing you how you would go about Getting IP from Website or Domain Name in C#. Reading and knowing this Project is very Simple, before we get into any code, you need to understand that this tutorial is not to advanced tutorial, using System.Net namespace. If you are not familiar with this item, I suggest you study them before attempting this tutorial.

Getting IP from Hostname is not so advanced but it is in intermediate level. This tutorial is not to long it is too short. And so this tutorial is not taking you time.
I will start by showing the Getting IP Function, Passing tow parameters.
So Let’s Began:
First of All Import The namespace
CODE

using System.Net;

The System.Net namespace provides a simple programming interface for many of the rotocols used on networks today. The WebRequest and WebResponse classes form the basis of what are called pluggable protocols, an implementation of network services that enables you to develop applications that use Internet resources without worrying about the specific details of the individual protocols.

Create the Global variables
Provides a container class for Internet host address information.
Provides an Internet Protocol (IP) address.

CODE

public IPHostEntry _IPHostEntry;    
public IPAddress[] _IPAddress;

So let’s Create our function GetIP
System.Enviroment namespace is used to get information from computer
ex (Computer Name, UserName, UserDomainName etc...)
CODE

private static string  GetIP (String _HostName,String _Return)
        {
            try
            {

                String myName = System.Environment.UserName;
                IPHostEntry _IPHostEntry;
                IPAddress[] _IPAddress;

                if (_HostName == "" || _HostName == null)
                {                  
                _HostName = “localhost”
                }

                if (!_HostName.Contains("@") || !_HostName .Contains(".com"))
                {
                    if (_HostName != myName )
                    {
                        _HostName  = "www." + _HostName.ToLower() + ".com";
                    }
if (_HostName.ToLower() == myName.ToLower() || _HostName.ToLower()  == "localhost")
                    {
                        {
                            _HostName = "localhost";
                        }
                    }


                    _IPHostEntry = Dns.GetHostByName(_HostName);
                    _IPAddress = _IPHostEntry.AddressList;
                    for (int i = 0; i < _IPAddress.Length; i++)
                    {
                        _Return = _IPAddress[0].ToString();
                    }
                    
                }
                return _Return;
            }
            catch (Exception ex)
            {
                _Return  = "";
                String StrErr;
                StrErr = ex.Message;
                if (StrErr.Length > 35)
                {
                    StrErr = StrErr.Substring(0, 35) + "...";
                }
                return  StrErr;
            }
        }


And then get our computer information
Provides information about, and means to manipulate,the current environment and platform. This class cannot be inherited.

CODE

private void ComInfo()
        {      
            Username = System.Environment.UserName.ToString();
            ComName  = System.Environment.MachineName.ToString();
//set username and computer name lables
            lblcomname.Text = ComName;
            lbluname.Text = Username;
        }

Get our local And WAN ips
Dnd.GetHostName() Provides simple domain name resolution functionality.

CODE

  private void GetLocalIP()
        {
            try
            {
                string _LocalIP = Dns.GetHostName();
               _IPHostEntry = Dns.GetHostByName(_LocalIP);
               _IPAddress = _IPHostEntry.AddressList;
                for (int i = 0; i < _IPAddress.Length; i++)
                {
                    lbllocalip.Text = _IPAddress[0].ToString();
                }




download in from the target location it means that first it runs the page then download the information or ip and inisalize it to a string

CODE

                String WanIP;
                WanIP = new System.Net.WebClient().DownloadString(("http://www.whatismyip.com/automation/n09230945.asp"));
                lbllocalip.Text = lbllocalip.Text + "  |  " + WanIP.ToString();
            }
            catch (Exception ex)
            {
                lblstatus.Text = ex.Message;
            }

        }



Attached File(s)
Attached File  Host_Name_To_IP.zip ( 139.53k ) Number of downloads: 120
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 12:36PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month