Get IP Address and Hostname using Visual Basic and Asp.Net

Posted by Samath
  2330
March 29, 2015

 

This is a simple program that fetch the IP Address of a system using the hostname. The program could be modify to fetch the IP address of any host. What it basically does is use the DNS class in the .Net Framework to get the info.

The Dns class is a static class that retrieves information about a specific host from the Internet Domain Name System (DNS).

so for example instead of doing what I did below in the example, you could get the IP address of a website by passing in the website name as argument instead of the local host name.

Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
    
    <style type="text/css">
        .auto-style1 {
            width: 100%;
            vertical-align: top;
        }
    </style>
    
</head>
<body>
    <form id="form1" runat="server">
    <div style="width: 278px">

        <table class="auto-style1" align="left">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Host Name: "></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblhostname" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label4"  runat="server" Text="IP Address: "></asp:Label>
                </td>
                <td>
                    <asp:ListBox ID="lstipaddress" runat="server"></asp:ListBox>
                </td>
            </tr>
          
        </table>
        <asp:Button ID="btngetip" runat="server" Text="Get IP Address and Host Name" />

    </div>
    </form>
</body>
</html>

 

.vb code:

Imports System.Data
Imports System.Net

Partial Class _Default
    Inherits System.Web.UI.Page


    Protected Sub btngetip_Click(sender As Object, e As EventArgs) Handles btngetip.Click
        Dim host_name As String
        Dim i As System.Net.IPHostEntry
        host_name = System.Net.Dns.GetHostName
        i = System.Net.Dns.GetHostByName(host_name)

        lblhostname.Text = i.HostName

        For Each b As System.Net.IPAddress In i.AddressList
            lstipaddress.Items.Add(b.ToString)
        Next

    End Sub
End Class

 

   
  
 
 
   

 
Flamez53
April 06, 2015
 
Hmmm not a bad...not a bad my boy
Theoboi
November 21, 2015
 
Must say its quite handy