001package edu.pdx.cs410J.net; 002 003import java.io.*; 004import java.net.*; 005 006/** 007 * A <code>Speaker</code> sends strings over a <code>Socket</code> to 008 * a <code>Listener</code>. 009 */ 010public class Speaker { 011 private static PrintStream err = System.err; 012 013 /** 014 * Reads the host name and port number, as well as the strings to 015 * send, from the command line. 016 */ 017 public static void main(String[] args) { 018 String host = args[0]; 019 int port = 0; 020 021 try { 022 port = Integer.parseInt(args[1]); 023 024 } catch (NumberFormatException ex) { 025 err.println("** Bad port number: " + args[1]); 026 System.exit(1); 027 } 028 029 try { 030 Socket socket = new Socket(host, port); 031 032 // Send some strings over the socket 033 OutputStreamWriter osw = 034 new OutputStreamWriter(socket.getOutputStream()); 035 PrintWriter speaker = new PrintWriter(osw); 036 037 for (int i = 2; i < args.length; i++) { 038 speaker.println(args[i]); 039 } 040 041 speaker.close(); 042 043 } catch (UnknownHostException ex) { 044 err.println("** Could not connect to host: " + host); 045 046 } catch (IOException ex) { 047 err.println("** IOException: " + ex); 048 System.exit(1); 049 } 050 } 051 052}