Explain Simple Echo Server

  • 2 Feb, 2012

    2. Echo serverWrite a server. This server is very similar to the echo server running on port 7. Basically, the echo server receives text from the client and then sends that exact text back to the client. This is just about the simplest server you can write. Note that this server handles only one client. Try to modify it to handle multiple clients using threads.import java.io.*;import java.net.*;public class echo3 {public static void main(String args[]) {// declaration section:// declare a server socket and a client socket for the server// declare an input and an output streamServerSocket echoServer = null;String line;DataInputStream is;PrintStream os;Socket clientSocket = null;// Try to open a server socket on port 9999// Note that we can't choose a port less than 1023 if we are not// privileged users (root)try {echoServer = new ServerSocket(9999);}catch (IOException e) {System.out.println(e);}// Create a socket object from the ServerSocket to listen and accept// connections.// Open input and output streamstry {clientSocket = echoServer.accept();is = new DataInputStream(clientSocket.getInputStream());os = new PrintStream(clientSocket.getOutputStream());// As long as we receive data, echo that data back to the client.while (true) {line = is.readLine();os.println(line);}}catch (IOException e) {System.out.println(e);}}}

    Comments Received:
    Please give your suggestions and feedback:

2009-2016 downloadmela.com. All rights reserved.