65.9K
CodeProject is changing. Read more.
Home

IP Server

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.14/5 (5 votes)

Mar 3, 2009

CPOL

1 min read

viewsIcon

27992

downloadIcon

1287

An IP server that pings a range of IP addresses.

Introduction

This is a client/server architecture program in which a client connects to a server using an IP and port number. The client sends a range of IP addresses to the server. The server receives the request and pings the IP range; it then sends the status of all IPs to the client. The client displays the incoming results. Many clients can connect concurrently to the server.

The Client

The application can be used by network administrators to discover the status of network nodes and troubleshoot links to hosts. At a lower level, clients connect to the server using TCP. The server checks the validity of the IP range and writes the IP range to a byte array. It attaches a stream to the client, writes the byte array to the stream, blocks the client, and waits for results. The client receives the results from the server and displays the results to the list view.

The Server

The server starts listening to port 2000. It accepts incoming connections and receives the IP range from the client to a byte array. It fetches and splits the IP range, initializes the Ping class, pings each IP address in the range, store the ping results (Online/Offline), and sends the results to the client.

Using the Code: The Client

The function Connect(IP Address, Port Number) creates a socket to connect to a remote server.

public TcpClient theClient;
Stream theStream;

try
{
    theClient = new TcpClient();
    theClient.Connect(txtServer.Text, Convert.ToInt32(txtPort.Text));
}
            
catch (Exception ex)
{
    MessageBox.Show("Can not reach server, please try again \n" + 
                    ex.Message, "Error Message",
    MessageBoxButtons.OK, MessageBoxIcon.Error);
}

theStream = theClient.GetStream();
String str = txtFrom.Text.Trim() + "|" + txtTo.Text.Trim();
ASCIIEncoding asen = new ASCIIEncoding();
byte[] ba = asen.GetBytes(str);

theStream.Write(ba, 0, ba.Length);//Write the bytes to the stream

byte[] bb = new byte[10000];
int k = theStream.Read(bb, 0, 10000);

Using the Code: The Server

public static String myAddress;

IPAddress ipAddress = IPAddress.Parse("127.0.0.1");    
TcpListener theList = new TcpListener(ipAddress, 2000);

while (true)
{
    theList.Start(); 
    Socket s = theList.AcceptSocket;
    byte[] b = new byte[100];
    int k = s.Receive(b);
}

for (int i = 0; i < k; i++)
{
    myAddress = myAddress + (Convert.ToChar(b[i]).ToString());
} 

for (int x = int.Parse(fromFourth); x <= int.Parse(toFourth); x++)
{
    ping.Open();

    TimeSpan span = ping.Send(fromFirst.ToString() + "." + fromSecond.ToString() + "." +    
    fromThird.ToString() + "." + x.ToString(), new TimeSpan(0, 0, 5));

    ping.Close();

   if (span == TimeSpan.MaxValue)
      myMessage = myMessage + (fromFirst.ToString() + "." + fromSecond.ToString() + "." +
      fromThird.ToString() + "." + x.ToString() + "     Offline\n");
   else
       myMessage = myMessage + (fromFirst.ToString() + "." + fromSecond.ToString() + "." +
       fromThird.ToString() + "." + x.ToString() + "     Online\n");
}

ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes(myMessage));