Skip to main content
Home
Live to Learn!

Main navigation

  • Home
  • Learn
    • FoxPro
    • MS-DOS
    • C PRG
    • Java
    • ASP
    • Ruby on Rails
    • ASP.NET
    • E-Books
    • Exam Preparation
    • Tools
  • Blog
  • Forums
  • Contact
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Learn by Example - Java Sample Programs

Ping server and Client in Java

Book navigation

  • Armstrong Number
  • Palindrome Checking
  • Playing Audio Clip using Applet
  • Applet Form
  • Exception Handling
  • Multiple Inheritance and Packages
  • Shopping List
  • Simple Multithread program
  • JDBC Program Select, Insert, Update, Delete records
  • RMI - Example AddServer
  • Simple Bank Account Process
  • TCP Server and Client in Java
  • UDP Server and Client in Java
  • FTP Server and Client in Java
  • Chat Server and Client in Java
  • Echo Server and Client in Java
  • Address Resolution Protocol in Java
  • Ping server and Client in Java
  • Multicast Server and Client in Java
  • Transposition Cipher Method
  • Poly-alphabetic Cipher Method Encryption - Java
  • DES - Using Data Encryption Standard in Java
  • AES - Using Advanced Encryption Standard in Java
  • Bit Stuffing
By Karthikeyan , 11 November, 2012

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 : 

  • Add new comment

Comments

Featured Blog Posts

Convert Currency in Number to Words (Indian Rupees) - MS Excel
Foxpro Tutorial and Programs
Convert Currency in Number to Words in MS Word
Convert Currency in Number to Words (Indian Rupees) - Version 2
Best way to Use Rupee Symbol in Windows – Easy steps
Convert Currency in Number to Words - MS Access
Creating All in One Windows XP DVD with all Important Applications
RSS feed

© 2009-2025 Live to Learn.In

Terms of Use | Privacy Policy