programming-examples/java/CLChat Client.java
2019-11-18 13:43:20 +01:00

138 lines
5.3 KiB
Java

CLChat Client
import java.net.*;
import java.io.*;
public class CLChatClient {
static final int DEFAULT_PORT = 1728; // Port where server is
// listening, if no
// port is specified
// on the command line.
static final String HANDSHAKE = "CLChat"; // Handshake string.
// Each end of the connection sends this string
// to the other just after the connection is
// opened. This is done to confirm that the
// program on the other side of the connection
// is a CLChat program.
static final char MESSAGE = '0'; // This character is prepended
// to every message that is sent.
static final char CLOSE = '1'; // This character is sent to
// the connected program when
// the user quits.
public static void main(String[] args) {
String computer; // The computer where the server is running,
// as specified on the command line. It can
// be either an IP number or a domain name.
int port; // The port on which the server listens.
Socket connection; // For communication with the server.
TextReader incoming; // Stream for receiving data from server.
PrintWriter outgoing; // Stream for sending data to server.
String messageOut; // A message to be sent to the server.
String messageIn; // A message received from the server.
/* First, get the computer from the command line.
Get the port from the command line, if one is specified,
or use the default port if none is specified. */
if (args.length == 0) {
TextIO.putln("Usage: java SimpleClient <computer-name> [<port>]");
return;
}
computer = args[0];
if (args.length == 1)
port = DEFAULT_PORT;
else {
try {
port= Integer.parseInt(args[1]);
if (port <= 0 || port > 65535)
throw new NumberFormatException();
}
catch (NumberFormatException e) {
TextIO.putln("Illegal port number, " + args[1]);
return;
}
}
/* Open a connetion to the server. Create streams for
communication and exchange the handshake. */
try {
TextIO.putln("Connecting to " + computer + " on port " + port);
connection = new Socket(computer,port);
incoming = new TextReader(connection.getInputStream());
outgoing = new PrintWriter(connection.getOutputStream());
outgoing.println(HANDSHAKE);
outgoing.flush();
messageIn = incoming.getln();
if (! messageIn.equals(HANDSHAKE) ) {
throw new IOException("Connected program is not CLChat!");
}
TextIO.putln("Connected. Enter your first message.
");
}
catch (Exception e) {
TextIO.putln("An error occurred while opening connection.");
TextIO.putln(e.toString());
return;
}
/* Exchange messages with the other end of the connection
until one side or the other closes the connection.
This client program send the first message. After that,
messages alternate strictly back an forth. */
try {
while (true) {
TextIO.put("SEND: ");
messageOut = TextIO.getln();
if (messageOut.equalsIgnoreCase("quit")) {
// User wants to quit. Inform the other side
// of the connection, then close the connection.
outgoing.println(CLOSE);
outgoing.flush();
connection.close();
TextIO.putln("Connection closed.");
break;
}
outgoing.println(MESSAGE + messageOut);
outgoing.flush();
if (outgoing.checkError()) {
throw new IOException("Error ocurred while reading incoming message.");
}
TextIO.putln("WAITING...");
messageIn = incoming.getln();
if (messageIn.length() > 0) {
// The first character of the message is a command.
// If the command is CLOSE, then the connection
// is closed. Otherwise, remove the command
// character from the message and procede.
if (messageIn.charAt(0) == CLOSE) {
TextIO.putln("Connection closed at other end.");
connection.close();
break;
}
messageIn = messageIn.substring(1);
}
TextIO.putln("RECEIVED: " + messageIn);
}
}
catch (Exception e) {
TextIO.putln("Sorry, an error has occurred. Connection lost.");
TextIO.putln(e.toString());
System.exit(1);
}
} // end main()
} //end class CLChatClient