Skip to content
38 changes: 29 additions & 9 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.io.*;
import client.*;
import common.*;

/**
* This class constructs the UI for a chat client. It implements the
* chat interface in order to activate the display() method.
Expand Down Expand Up @@ -41,11 +40,11 @@ public class ClientConsole implements ChatIF
* @param host The host to connect to.
* @param port The port to connect on.
*/
public ClientConsole(String host, int port)
public ClientConsole(String loginid,String host, int port)
{
try
{
client= new ChatClient(host, port, this);
client= new ChatClient(loginid, host, port, this);
}
catch(IOException exception)
{
Expand Down Expand Up @@ -100,23 +99,44 @@ public void display(String message)
/**
* This method is responsible for the creation of the Client UI.
*
* @param args[0] The host to connect to.
* @param args[0] The login id
* @param args[1] The host to connect to.
* @param args[2] The port to connect to.
*/
public static void main(String[] args)
{
String host = "";
int port = 0; //The port number

int port ; //The port number
String loginid="";
try{
loginid=args[0];
}
catch(Exception e){
System.out.println("ERROR - No login ID specified. Connection aborted.");
System.exit(0);
return;
}

try
{
host = args[0];
host = args[1];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
chat.accept(); //Wait for console data

try
{
port = Integer.parseInt(args[2]);
}
catch(ArrayIndexOutOfBoundsException e)
{
port = DEFAULT_PORT;
}

ClientConsole cc= new ClientConsole(loginid,host, port);
cc.accept();
}
}
//End of ConsoleChat class
221 changes: 161 additions & 60 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,106 +4,207 @@

import java.io.*;
import ocsf.server.*;
import common.*;
import java.util.Scanner;

/**
* This class overrides some of the methods in the abstract
* superclass in order to give more functionality to the server.
* This class overrides some of the methods in the abstract superclass in order
* to give more functionality to the server.
*
* @author Dr Timothy C. Lethbridge
* @author Dr Robert Laganière
* @author François Bélanger
* @author Paul Holden
* @version July 2000
*/
public class EchoServer extends AbstractServer
{
//Class variables *************************************************

public class EchoServer extends AbstractServer {
// Class variables *************************************************

/**
* The default port to listen on.
*/
final public static int DEFAULT_PORT = 5555;
//Constructors ****************************************************

// Constructors ****************************************************

/**
* Constructs an instance of the echo server.
*
* @param port The port number to connect on.
*/
public EchoServer(int port)
{
public EchoServer(int port) {
super(port);

}


//Instance methods ************************************************

// Instance methods ************************************************

/**
* This method handles any messages received from the client.
*
* @param msg The message received from the client.
* @param msg The message received from the client.
* @param client The connection from which the message originated.
*/
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
public void handleMessageFromClient(Object msg, ConnectionToClient client) {
String message = (String) msg;
String[] mes = message.split(" ");
try {
if (mes[0].equals("#login")) {
if (client.getInfo("loginid") == null) {
String loginid = mes[1];
client.setInfo("loginid", loginid);
this.sendToAllClients(client.getInfo("loginid") + " logged in");

} else {

client.sendToClient("Error, please try again!");
client.close();
}
}
} catch (Exception ex) {
}
System.out.println("Message received: " + msg + " from " + client.getInfo("loginid"));

this.sendToAllClients(client.getInfo("loginid") + "> " + msg);

}


public void handleMessageFromServerUI(String message) {

if (message.charAt(0)=='#') {
switch (message.substring(0)) {
case "#close":
sendToAllClients("SERVER SHUTTING DOWN! DISCONNECTING!");
serverClosed();
break;
case "#stop":
serverStopped();
sendToAllClients("WARNING - Server has stopped listening for connections.");
break;
case "#quit":
try {
close();
} catch (IOException e) {

}
System.out.println("server quits...");
break;
case "#setport":
if (!isListening()) {
try {
int port = Integer.parseInt(message.substring(9));
setPort(port);
System.out.println("Port is set to: " + port);
} catch (Exception e) {
System.out.println("invalid port, please try again");
return;
}
} else {
System.out.println("You need to close server before set port");
return;
}
break;
case "#start":
if (!isListening()) {
try {
listen();
System.out.println("^^ logging you in ...^^");
} catch (Exception e) {
System.out.println("Exception occurred when log in. please try again");
return;
}
} else {
System.out.println("You need to close server before start server");
return;
}
break;
case "#getport":
System.out.println("Your current port is: " + getPort());
break;
default:
System.out.println("Invalid command");
break;
}
} else {
System.out.println(message);
this.sendToAllClients("SERVER MSG> " + message);

}
}

/**
* This method overrides the one in the superclass. Called
* when the server starts listening for connections.
* This method overrides the one in the superclass. Called when the server
* starts listening for connections.
*/
protected void serverStarted()
{
System.out.println
("Server listening for connections on port " + getPort());
@Override
protected void serverStarted() {
System.out.println("Server listening for connections on port " + getPort());
}

/**
* This method overrides the one in the superclass. Called
* when the server stops listening for connections.
* This method overrides the one in the superclass. Called called when the
* server is closed.
*/
protected void serverStopped()
{
System.out.println
("Server has stopped listening for connections.");
@Override
protected void serverClosed() {
System.out.println("Server has closed.");
}

//Class methods ***************************************************


@Override
protected void clientConnected(ConnectionToClient client) {
System.out.println("A new client is attempting to connect to the server.");
}

@Override
protected synchronized void clientDisconnected(ConnectionToClient client) {
System.out.println(client.getInfo("loginid") + " has disconnected.");
sendToAllClients(client.getInfo("loginid") + " has disconnected.");

}

@Override
protected synchronized void clientException(ConnectionToClient client, Throwable exception) {
System.out.println(client.getInfo("loginid") + " has disconnected.");
sendToAllClients(client.getInfo("loginid") + " has disconnected.");
}

/**
* This method is responsible for the creation of
* the server instance (there is no UI in this phase).
* This method overrides the one in the superclass. Called when the server stops
* listening for connections.
*/
@Override
protected void serverStopped() {
System.out.println("Server has stopped listening for connections.");
}

// Class methods ***************************************************

/**
* This method is responsible for the creation of the server instance (there is
* no UI in this phase).
*
* @param args[0] The port number to listen on. Defaults to 5555
* if no argument is entered.
* @param args[0] The port number to listen on. Defaults to 5555 if no argument
* is entered.
*/
public static void main(String[] args)
{
int port = 0; //Port to listen on
public static void main(String[] args) {
int port = 0; // Port to listen on

try
{
port = Integer.parseInt(args[0]); //Get port from command line
try {
port = Integer.parseInt(args[0]); // Get port from command line
} catch (Throwable t) {
port = DEFAULT_PORT; // Set port to 5555
}
catch(Throwable t)
{
port = DEFAULT_PORT; //Set port to 5555
}


EchoServer sv = new EchoServer(port);

try
{
sv.listen(); //Start listening for connections
}
catch (Exception ex)
{
ServerConsole sc = new ServerConsole(sv);

try {
sv.listen(); // Start listening for connections
} catch (Exception ex) {
System.out.println("ERROR - Could not listen for clients!");
}
sc.accept();
}

}
//End of EchoServer class
// End of EchoServer class
Loading