Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 49 additions & 25 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,13 @@ 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)
{
try
{
client= new ChatClient(host, port, this);
}
catch(IOException exception)
{
System.out.println("Error: Can't setup connection!"
+ " Terminating client.");
System.exit(1);
}
public ClientConsole(String username, String host, int port) {
try {
client = new ChatClient(username, host, port, this);
}
catch (IOException exception) {
System.out.println("Cannot establish connection on port" + port + ". Input a command.");
}
}


Expand All @@ -79,10 +74,13 @@ public void accept()
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
("Terminating client.");
}
}




/**
* This method overrides the method in the ChatIF interface. It
* displays a message onto the screen.
Expand All @@ -91,7 +89,7 @@ public void accept()
*/
public void display(String message)
{
System.out.println("> " + message);
System.out.println(" > " + message);
}


Expand All @@ -104,19 +102,45 @@ public void display(String message)
*/
public static void main(String[] args)
{
String host = "";
int port = 0; //The port number
String host;
int port;
String username;

try
{
host = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
/*
* Modified for E51
* Require a login ID
*/
try {
username = args[0];
}
catch (ArrayIndexOutOfBoundsException e) {
System.err.println("A userID is required to proceed.");
System.exit(1);
return;
}

try {
host = args[1];
} catch (ArrayIndexOutOfBoundsException e) {
host = "localhost";
}

/*
* Modified for E49
* Allow more than just the default port
*/
try {
port = Integer.parseInt(args[2]);
}
catch (Exception e) {
port = DEFAULT_PORT;
}

ClientConsole chat = new ClientConsole(username, host, port);
chat.accept(); //Wait for console data
}
}



//End of ConsoleChat class
115 changes: 113 additions & 2 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
// license found at www.lloseng.com

import java.io.*;
import client.*;
import ocsf.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 Dr Robert Laganiegrave;re
* @author Franccedil;ois Beacute;langer
* @author Paul Holden
* @version July 2000
*/
Expand Down Expand Up @@ -48,9 +51,25 @@ public EchoServer(int port)
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{

System.out.println("Message received: " + msg + " from " + client);
this.sendToAllClients(msg);
}

public void clientDisconnected(ConnectionToClient client){

String msg = "Client disconnet at " + client;
System.out.println(msg);
this.sendToAllClients(msg);
}

public void clientConnected(ConnectionToClient client) {


String msg = "Client connected at " + client;
System.out.println(msg);
this.sendToAllClients(msg);
}

/**
* This method overrides the one in the superclass. Called
Expand All @@ -70,7 +89,88 @@ protected void serverStopped()
{
System.out.println
("Server has stopped listening for connections.");
this.sendToAllClients(null);
}

public void handleMessageFromServerUI(String message) {
if (message.startsWith("#")) {
String[] index = message.split(" ");
String command = index[0];
switch (command) {
case "#quit":
//closes the server and then exits it
try {
this.close();
} catch (IOException e) {
System.exit(1);
}
System.exit(0);
break;
case "#stop":
this.sendToAllClients("#stop");
this.stopListening();
break;
case "#close":
try {
this.close();
} catch (IOException e) {
}
break;
case "#setport":
if (!this.isListening() && this.getNumberOfClients() < 1) {
super.setPort(Integer.parseInt(index[1]));
System.out.println("Port set to " + Integer.parseInt(index[1]));
} else {
System.out.println("Connection already Established.");
}
break;
case "#start":
if (!this.isListening()) {
try {
this.listen();
} catch (IOException e) {
//error listening for clients
}
} else {
System.out.println("Server is up and running.");
}
break;
case "#getport":
System.out.println("Current port is " + this.getPort());
break;
default:
System.out.println("The command '" + command+ "' is not recognized.");
break;
}
} else {
this.sendToAllClients(message);
}
}

public void accept(){ //method for taking input

try
{
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
String message;

while (true)
{
message = fromConsole.readLine();
handleMessageFromServerUI(message);
}
}
catch (Exception ex)
{
System.out.println
("Terminating.");
}
}





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

Expand Down Expand Up @@ -104,6 +204,17 @@ public static void main(String[] args)
{
System.out.println("ERROR - Could not listen for clients!");
}

sv.accept();
//serverClient.accept();
}








}
//End of EchoServer class
Loading