Click here to Skip to main content
Click here to Skip to main content

IP Server

, 3 Mar 2009
Rate this:
Please Sign up or sign in to vote.
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));

License

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

About the Author

AKA MAJO
Software Developer
Lebanon Lebanon
Works in a multinational pharmaceutical company as an IT specialist. A freelance software developer and web designer.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 3 Mar 2009
Article Copyright 2009 by AKA MAJO
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid