Click here to Skip to main content
11,718,289 members (83,937 online)
Click here to Skip to main content

Test TCP/IP Connectivity Through aspx Page

, 9 Jul 2013 CPOL 16.5K 1.3K 19
Rate this:
Please Sign up or sign in to vote.
Test TCP/IP connectivity through aspx page

Introduction

Recently, I came across the requirement where I needed to test TCP/IP port connectivity from website hosted on IIS server to another third party server. So I created this small utility which tests the TCP/IP port connectivity using .aspx page. The good thing about this code is that it’s very portable as all the code is written in aspx page (inline code) with no code behind file which does not require any code build.

Using the Utility

Below is a brief description of how to use this utility.

Get URL IP

To get the IP of the URL you need paste the URL inside text box and click on GetIP button as shown in the below screenshot.

//Code for GetUrlIP method
void GetUrlIP(string serverUrl)
    {
        try
        {
            Uri uri = new Uri(serverUrl);
            IPHostEntry hostName = Dns.GetHostByName(uri.Host);
            string ip = hostName.AddressList[0].ToString();
            divMsg.InnerHtml = "IP of " + uri.Host + ": " + ip;
        }
        catch (Exception ex)
        {
            divMsg.InnerHtml += ex.Message;
        }
    }

PingServer

To get the ping status of an IP, you need to put IP inside the textbox and click on Ping button as shown in the below screenshot:

//Code for PingServer method
void PingServer(string ipAddress)
{
    ipAddress = ipAddress.Trim();
    Ping p = new Ping();
    try
    {
        IPAddress.Parse(ipAddress);
        PingReply reply = p.Send(ipAddress, 3000);
        divMsg.InnerHtml += "Pinging " + ipAddress + " with 32 bytes of data :<br>";
        
        if (reply.Status == IPStatus.Success)
        {
            divMsg.InnerHtml += "Reply from " + reply.Address.ToString() + 
            ": bytes=" + reply.Buffer.Length.ToString() + "bytes time=" + 
            reply.RoundtripTime.ToString() + "ms TTL=" + reply.Options.Ttl.ToString();
        }
        else if (reply.Status == IPStatus.TimedOut)
        {
            divMsg.InnerHtml += "Request time out.";
        }
    }
    catch (Exception ex)
    {
        divMsg.InnerHtml += ex.Message;
    }
}

ConnectToServerPort

To test TCP/IP connectivity, you need put the IP address and port number in text box and click on Test connectivity button as shown in the below screenshot:

void ConnectToServerPort(string ipAddress, int port)
{
    try
    {
        ipAddress = ipAddress.Trim();
        IPAddress IP = IPAddress.Parse(ipAddress);
        Socket s = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream,
        ProtocolType.Tcp);
        
        divMsg.InnerHtml += "Connecting to " + ipAddress + 
        " on port " + port.ToString() + "<br>";
        s.Connect(IP, port);
        if (s.Connected)
        {
            divMsg.InnerHtml += "Connected Successfully";
            s.Disconnect(false);
        }
        else
        {
            divMsg.InnerHtml += "Connection Failed";
        }
    }
    catch (Exception ex)
    {
        divMsg.InnerHtml += "Connection Failed" + "<br>";
        divMsg.InnerHtml += ex.Message;
    }
}

Deploying Code To IIS

For using this utility with your existing ASP.NET web site, you simply need to copy TestTcpIpConnectivity.aspx into your web site root folder. Afterwards, you can access it by using http://yourwebsite/TestTcpIpConnectivity.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mahesh Bailwal
Software Developer (Senior)
India India
Developer

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Nandakishorerao12-Jul-13 2:13
memberNandakishorerao12-Jul-13 2:13 
GeneralRe: My vote of 5 Pin
Mahesh Bailwal12-Jul-13 6:12
professionalMahesh Bailwal12-Jul-13 6:12 
GeneralMy vote of 4 Pin
H. Mueller10-Jul-13 20:40
professionalH. Mueller10-Jul-13 20:40 
GeneralRe: My vote of 4 Pin
Mahesh Bailwal11-Jul-13 0:43
professionalMahesh Bailwal11-Jul-13 0:43 
GeneralMy vote of 5 Pin
adkalavadia10-Jul-13 19:27
memberadkalavadia10-Jul-13 19:27 
GeneralRe: My vote of 5 Pin
Mahesh Bailwal11-Jul-13 0:43
professionalMahesh Bailwal11-Jul-13 0:43 
GeneralMy vote of 5 Pin
newton.saber10-Jul-13 3:57
membernewton.saber10-Jul-13 3:57 
GeneralRe: My vote of 5 Pin
Mahesh Bailwal10-Jul-13 5:11
professionalMahesh Bailwal10-Jul-13 5:11 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150901.1 | Last Updated 10 Jul 2013
Article Copyright 2013 by Mahesh Bailwal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid