*** ECHO CLIENT *** import java.net.*; import java.io.*; public class echoClient { public static void main(String[] args) { Socket theSocket; String hostname; DataInputStream theInputStream; DataInputStream userInput; PrintStream theOutputStream; String theLine; if (args.length > 0) { hostname = args[0]; } else { hostname = "LOCALHOST"; } try { theSocket = new Socket(hostname, 9999); theInputStream = new DataInputStream(theSocket.getInputStream()); theOutputStream = new PrintStream(theSocket.getOutputStream()); userInput = new DataInputStream(System.in); while (true) { theLine = userInput.readLine(); //System.out.println("?"+theLine); if (theLine.equals(".")) break; theOutputStream.println(theLine); //System.out.println(theInputStream.readLine()); } } // end try catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } } // end main } // end echoClient *** ECHO 3 *** import java.io.*; import java.net.*; class echoThread extends Thread { String line; DataInputStream is; PrintStream os; Socket clientSocket; public echoThread(Socket cl) { clientSocket = cl; } public void run() { try { is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); while(true) { line = is.readLine(); os.println(line); } } catch(IOException e) { System.out.println(e); } } } 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 stream ServerSocket 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 streams //try { // 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); System.out.println(line); } // } // catch (IOException e) { // System.out.println(e); // } } } *** ECHO 4 *** import java.io.*; import java.net.*; class echoThread extends Thread { String line; DataInputStream is; PrintStream os; Socket clientSocket; public echoThread(Socket cl) { clientSocket = cl; } public void run() { try { is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); while(true) { line = is.readLine(); os.println(line); System.out.println(line); } } catch(IOException e) { System.out.println(e); } } } public class echo4 { public static void main(String args[]) { Socket clientSocket; ServerSocket echoServer = null; try { echoServer = new ServerSocket(9999); } catch (IOException e) {} try { while(true) { clientSocket = echoServer.accept(); echoThread et = new echoThread(clientSocket); et.start(); } } catch (Exception e) {} } } *** CHAT *** import java.io.*; import java.net.*; import java.util.*; class echoThread extends Thread { String line; DataInputStream is; PrintStream os; Socket clientSocket; public echoThread(Socket cl) { clientSocket = cl; } public void run() { try { is = new DataInputStream(clientSocket.getInputStream()); os = new PrintStream(clientSocket.getOutputStream()); while(true) { line = is.readLine(); os.println(line); System.out.print(clientSocket.getInetAddress().getHostName() +">"); System.out.println(line); } } catch(IOException e) { System.out.println(e); } } } public class Chat { public static void main(String args[]) { Vector clientName = new Vector(); Socket clientSocket; ServerSocket echoServer = null; try { echoServer = new ServerSocket(9999); } catch (IOException e) {} try { while(true) { clientSocket = echoServer.accept(); echoThread et = new echoThread(clientSocket); et.start(); clientName.addElement(clientSocket.getInetAddress().getHostName()); } } catch (Exception e) {} } }