import java.io.*;
import java.net.*;
public class Client implements Runnable{
static Socket clientSocket = null;
static PrintStream os = null;
static DataInputStream is = null;
static BufferedReader inputLine = null;
static boolean closed = false;
public static void main(String[] args) {
// The default port
int port_number=2222;
String host="localhost";
if (args.length < 2)
{
System.out.println("Usage: java Client \n"+
"Now using host="+host+", port_number="+port_number);
} else {
host=args[0];
port_number=Integer.valueOf(args[1]).intValue();
}
try {
clientSocket = new Socket(host, port_number);
inputLine = new BufferedReader(new InputStreamReader(System.in));
os = new PrintStream(clientSocket.getOutputStream());
is = new DataInputStream(clientSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host "+host);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to the host "+host);
}
if (clientSocket != null && os != null && is != null) {
try {
new Thread(new Client()).start();
while (!closed) {
os.println(inputLine.readLine());
}
os.close();
is.close();
clientSocket.close();
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
public void run() {
String responseLine;
// Keep on reading from the socket till we receive the "Bye" from the server,
// once we received that then we want to break.
try{
System.out.println("Tryin.g...");
while ((responseLine = inputLine.readLine()) != null) {
System.out.println(responseLine);
if (responseLine.indexOf("*** Bye") != -1) break;
}
closed=true;
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
} |