Click here to Skip to main content
15,114,302 members
Articles / Programming Languages / C#
Posted 3 Mar 2009

Tagged as


25 bookmarked

IP Server

Rate me:
Please Sign up or sign in to vote.
4.14/5 (5 votes)
3 Mar 2009CPOL1 min read
An IP server that pings a range of IP addresses.


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;

    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("");    
TcpListener theList = new TcpListener(ipAddress, 2000);

while (true)
    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++)

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


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

ASCIIEncoding asen = new ASCIIEncoding();


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


About the Author

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 --