import java.io.*; import java.net.*; import java.util.*; public class TextSender { public static void main (String[] argv) { try { // Open a socket to the server: rabbit.seas.gwu.edu, port 9003. Socket soc = new Socket ("rabbit.cs.gwu.edu", 9003); // To test on same machine: Socket soc = new Socket ("localhost", 9003); // Note: server must be fired up first! InetAddress remoteMachine = soc.getInetAddress(); System.out.println ("Attempting connection to " + remoteMachine); // Now create the output stream and wrap a PrintWriter around it. OutputStream outStream = soc.getOutputStream (); PrintWriter pw = new PrintWriter (outStream); // Read text from the screen: Scanner scanner = new Scanner (System.in); System.out.print ("Enter text: "); while (scanner.hasNext()) { // Extract string and send it on the socket. String line = scanner.next(); pw.println (line); // Note: must flush() to actually cause line to be sent. pw.flush(); // Repeat. System.out.print ("Enter text: "); } } catch (IOException e) { System.out.println (e); } } }