Ping server and Client in Java

Submitted by Karthikeyan on

Ping server and Client in Java using Sockets

Server listens the incoming connections in port. Client sends a string to server. Server echoes the string received from the client and shows the client address. Client receives the echoed string and calculates the round trip time and data loss if any.

Ping Server


import java.io.*;
import java.net.*;
import java.util.*;
import java.text.*;
class pings {
    public static void main(String args[]) throws IOException
    {
        ServerSocket s = new ServerSocket(2000);
        while(true)
        {
            Socket c = s.accept();
            InputStream in = c.getInputStream();
            InputStreamReader inr = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(inr);
            String str = br.readLine();
            System.out.println("Ping command received from : "+c.getInetAddress() +" with string "+str);
            PrintStream ps = new PrintStream(c.getOutputStream());
            ps.println(str);
        }
    }
}

Ping Client


import java.io.*;
import java.net.*;
public class pingc {
    public static void main(String args[]) throws IOException
    {  
        long t1, t2;
        while(true)
        {
            try{
            Socket soc = new Socket("localhost",2000);
            InputStreamReader isr = new InputStreamReader(System.in);
            BufferedReader br = new BufferedReader(isr);
            System.out.println("Type a string to ping : ");
            String str = br.readLine();
            OutputStream os = soc.getOutputStream();
            PrintWriter pw = new PrintWriter(os,true);
            InputStream in = soc.getInputStream();
            InputStreamReader inr = new InputStreamReader(in);
            BufferedReader br1 = new BufferedReader(inr);
            t1 = System.currentTimeMillis();
            pw.println(str);
            
            String str1 = br1.readLine();
            t2 = System.currentTimeMillis();
            System.out.println("Pinging "+soc.getInetAddress()+" with string "+str );
            System.out.println("Reply from "+soc.getInetAddress() +" String "+str1+" Length : "+str1.length());
            System.out.println("Sent : "+str.length()+" Received : "+str1.length()+" Lost : "+(str.length()-str1.length()));
            System.out.println("Approx. Time in Milliseconds  = "+(t2-t1));
            }
            catch(Exception e)
            {
                System.out.println("Error : "+e.getMessage());
            }
        }
            
    }
 
}

Output :

Compile and run server program first and then client program Server Window

Ping command received from : /127.0.0.1 with string hello
Ping command received from : /127.0.0.1 with string test
Client Window

Type a string to ping : 
hello
Pinging localhost/127.0.0.1 with string hello
Reply from localhost/127.0.0.1 String hello Length : 5
Sent : 5 Received : 5 Lost : 0
Approx. Time in Milliseconds  = 5
Type a string to ping : 
test
Pinging localhost/127.0.0.1 with string test
Reply from localhost/127.0.0.1 String test Length : 4
Sent : 4 Received : 4 Lost : 0
Approx. Time in Milliseconds  = 4
Type a string to ping :