/* * Created on 04.04.2004 * * Example client implementation for lab12 lab */ import java.io.*; import java.net.*; /** * @author Daria Antonova * * Example client implementation for lab12 lab */ public class MyClient { public static void main(String[] args) throws IOException { Socket myClientSocket = null; PrintWriter outToServer = null; BufferedReader inFromServer = null; // name and port number of machine your will try to connect //String remote = new String("wasat.ccs.neu.edu"); String remote = new String("localhost"); int port = 6070; try { System.out.println("MyClient is trying to connect to "+ remote +" server..."); // create new socket object named myClientSocket where // "wasat" is the name of the nachine on the netwok, 6070 is the post number myClientSocket = new Socket(remote, port); // open PrintWriter on socket's output stream outToServer = new PrintWriter(myClientSocket.getOutputStream(), true); // open BufferReader on socket's input stream inFromServer = new BufferedReader(new InputStreamReader( myClientSocket.getInputStream())); // see Java I/O documentation to get familiar withh Java I/O classes } catch (UnknownHostException e) { System.err.println("Unknown host: " + remote + "."); System.exit(1); } catch (IOException e) { System.err.println("Error getting I/O for connection to " + remote + "."); System.exit(1); } // greate BufferedReader for user input on local machine BufferedReader stdIn = new BufferedReader( new InputStreamReader(System.in)); String userInput; // while there is input from user while ((userInput = stdIn.readLine()) != null) { if (userInput.compareTo("exit") == 0) { break; } outToServer.println(userInput); System.out.println("echo: " + inFromServer.readLine()); } outToServer.close(); inFromServer.close(); stdIn.close(); myClientSocket.close(); } }