import java.io.IOException; import java.io.PrintWriter; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.Socket; public class DateClient { public static void main(String[] args) throws IOException { String serverIP = "cs117.cs.gettysburg.edu"; int portNo = 8888; String clientName = "Linus"; // 0. choice of different server/portNo if (args.length > 0) { serverIP = args[0]; } if (args.length > 1) { portNo = Integer.parseInt(args[1]); } System.out.print("Connecting to " + serverIP + " ... "); // 1. connect to the server Socket clientSocket = new Socket(serverIP, portNo); System.out.println(" successful!"); // 2. setup communication channels PrintWriter toServer = new PrintWriter(clientSocket.getOutputStream()); // InputStream IS-NOT-An InputStreamReader, must wrap around BufferedReader fromServer = new BufferedReader( new InputStreamReader( clientSocket.getInputStream())); // 2.5 send client's name toServer.println(clientName); toServer.flush(); // force-send // 3. receive message from the server and print it to the screen String line = fromServer.readLine(); System.out.println(line); // 4. close connection fromServer.close(); toServer.close(); } }