Ping server and Client in Java

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

  1. span style="color: #006699;">java.io.*java.net.*java.util.*java.text.*"Ping command received from : "" with string "

Ping Client

  1. span style="color: #006699;">java.io.*java.net.*"localhost""Type a string to ping : ""Pinging "" with string ""Reply from "" String "+str1+" Length : ""Sent : "+str.length()+" Received : "+str1.length()+" Lost : ""Approx. Time in Milliseconds = ""Error : "+e.getMessage());
  2. }
  3. }
  4.  
  5. }
  6.  
  7. }

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 :