import java.io.*; import java.net.*; /** This program implements a simple server that listens to PORT and echoes back all client input. */ public class TCP_EchoServer { public static final int PORT = 8080; public static void main(String[] args ) { try { // establish server socket ServerSocket s = new ServerSocket(PORT); System.out.println("Started: " + s); // wait for client connection Socket incoming = s.accept( ); System.out.println("Connection accepted: "+ socket); BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter(incoming.getOutputStream(), true /* autoFlush */); out.println( "Hello! Enter BYE to exit." ); // echo client input boolean done = false; while (!done) { String line = in.readLine(); if (line == null) done = true; else { out.println("Echo: " + line); if (line.trim().equals("BYE")) done = true; } } incoming.close(); } catch (Exception e) { e.printStackTrace(); } } }