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
18 changes: 6 additions & 12 deletions code/simplechat1/ClientConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void accept()
while (true)
{
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);
client.handleMessageFromServerUI(message);
}
}
catch (Exception ex)
Expand Down Expand Up @@ -104,19 +104,13 @@ public void display(String message)
*/
public static void main(String[] args)
{
String host = "";
int port = 0; //The port number

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

port = Integer.parseInt(args[0]);

ServerConsole server= new ServerConsole(host, port);
server.accept(); //Wait for console data
}
}
//End of ConsoleChat class
36 changes: 32 additions & 4 deletions code/simplechat1/EchoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.io.*;
import ocsf.server.*;

import common.*;
/**
* This class overrides some of the methods in the abstract
* superclass in order to give more functionality to the server.
Expand All @@ -18,7 +18,7 @@
public class EchoServer extends AbstractServer
{
//Class variables *************************************************

ChatIF serverUI;
/**
* The default port to listen on.
*/
Expand All @@ -35,6 +35,7 @@ public EchoServer(int port)
{
super(port);
}



//Instance methods ************************************************
Expand All @@ -45,6 +46,12 @@ public EchoServer(int port)
* @param msg The message received from the client.
* @param client The connection from which the message originated.
*/
protected void clientConnected(ConnectionToClient client){
System.out.println("A client has connected to the server.");
}
synchronized protected void clientDisconnected(ConnectionToClient client){
System.out.println("A client has disconnected from the server");
}
public void handleMessageFromClient
(Object msg, ConnectionToClient client)
{
Expand All @@ -71,8 +78,29 @@ protected void serverStopped()
System.out.println
("Server has stopped listening for connections.");
}

//Class methods ***************************************************
@Override
protected void clientConnected(ConnectionToClient client)
{
System.out.println("Server is connected to " + client + ".")
}
synchronized protected void clientDisconnected(ConnectionToClient client)
{
System.out.println("Server has disconnected from " + client + ".")

}
public void handleMessageFromServerUI(String message){
if (message[0] ="#"){
if message.equals("#quit")
this.close();
else if message.equals("#stop")
this.stopListening();
else if message.equals("#close")
close();
else if message.equals("#getport")
clientUI.display(" The port is " + getPort());
else if message.equals("#start")
this.listen();
}

/**
* This method is responsible for the creation of
Expand Down
109 changes: 109 additions & 0 deletions code/simplechat1/ServerConsole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import java.io.*;
import client.*;
import common.*;


public class ServerConsole implements ChatIF
{
//Class variables *************************************************

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

//Instance variables **********************************************

/**
* The instance of the client that created this ConsoleChat.
*/
EchoServer server;


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

/**
* Constructs an instance of the ClientConsole UI.
*
* @param host The host to connect to.
* @param port The port to connect on.
*/
public ServerConsole(int port)
{
server = new EchoServer(port);
try
{
server.listen();
}
catch(IOException exception)
{
System.out.println("Error: cannot listen for clients");
System.exit(1);
}
}


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

/**
* This method waits for input from the console. Once it is
* received, it sends it to the client's message handler.
*/
public void accept()
{
try
{
BufferedReader fromConsole =
new BufferedReader(new InputStreamReader(System.in));
String message;

while (true)
{
message = fromConsole.readLine();
client.handleMessageFromClientUI(message);
}
}
catch (Exception ex)
{
System.out.println
("Unexpected error while reading from console!");
}
}

/**
* This method overrides the method in the ChatIF interface. It
* displays a message onto the screen.
*
* @param message The string to be displayed.
*/
public void display(String message)
{
System.out.println("> " + message);
}


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

/**
* This method is responsible for the creation of the Client UI.
*
* @param args[0] The host to connect to.
*/
public static void main(String[] args)
{
String host = "";
int port = 0; //The port number

try
{
host = args[0];
}
catch(ArrayIndexOutOfBoundsException e)
{
host = "localhost";
}
ClientConsole chat= new ClientConsole(host, DEFAULT_PORT);
chat.accept(); //Wait for console data
}
}
//End of ServerConsole class
29 changes: 23 additions & 6 deletions code/simplechat1/client/ChatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void handleMessageFromServer(Object msg)
* @param message The message from the UI.
*/
public void handleMessageFromClientUI(String message)
{
/*{
try
{
sendToServer(message);
Expand All @@ -76,14 +76,31 @@ public void handleMessageFromClientUI(String message)
("Could not send message to server. Terminating client.");
quit();
}
*/
if (message[0] ="#"){
if message.equals("#quit")
quit();
else if message.equals("#logoff")
closeConnection();
else if message.equals("#gethost")
clientUI.display(" The host is " + getHost());
else if message.equals("#getport")
clientUI.display(" The port is " + getPort());
// couldn't figure out how to implement the rest in time.
}
}
protected void connectionEstablished(){
System.out.println("Connection to the server has been established")
}
protected void connectionclosed(){
system.out.println("The server's connection has been closed")
}
protected void connectionException(Exception exception) {
//if connection is closed, throw this exception and print "The connections lost due to server shutting down"
//if connection is closed, throw this exception and print "The connections lost due to server shutting down"
System.out.println("The connection is lost due to server shutting down")
quit();
}
@Override
public void connectionClosed(){
//
}


/**
* This method terminates the client.
Expand Down