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

Echo 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 , 1 November, 2012

The echo server program in java that simply broadcasts the message received to all the clients

Echo Server Program


import java.io.*;
import java.net.*;
class echos {
    public static void main(String args[]) throws Exception
    {
        String echoin;
        ServerSocket svrsoc;
        Socket soc;
        BufferedReader br;
        try
        {
            svrsoc = new ServerSocket(2000);
            soc = svrsoc.accept();
            br = new BufferedReader (new InputStreamReader(soc.getInputStream()));
            PrintStream ps = new PrintStream(soc.getOutputStream());
            System.out.println("Connected for echo:");
            while((echoin=br.readLine())!=null)
            {
                if(echoin.equals("end"))
                {
                    System.out.println("Client disconnected");
                    br.close();
                    break;
                }
                else
                    ps.println(echoin);
            }
        } 
        catch(UnknownHostException e)
        {
            System.out.println(e.toString());
        }
        catch(IOException ioe)
        {
            System.out.println(ioe);
        }
    }    
}

Echo Client Program


import java.io.*;
import java.net.*;
class echoc {
public static void main(String args[]) throws Exception    
{
    String sockin;
    try
    {
        Socket csock = new Socket(InetAddress.getLocalHost(),2000);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedReader br_sock = new BufferedReader(new InputStreamReader(csock.getInputStream()));
        PrintStream ps = new PrintStream(csock.getOutputStream());
        System.out.println("Start echoing... type 'end' to terminate");
        while((sockin=br.readLine())!=null)
        {
            ps.println(sockin);
            if(sockin.equals("end"))
                break;
            else 
                System.out.println("echoed from server:"+br_sock.readLine());
            
        }
    }
catch(UnknownHostException e)
{
    System.out.println(e.toString());
    
}
catch(IOException ioe)
{
    System.out.println(ioe);
}
}
}

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