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
Binary file added ClientServerFramework/Client.class
Binary file not shown.
120 changes: 120 additions & 0 deletions ClientServerFramework/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/**
* Created by danielbardin on 4/6/15.
*/

import java.net.*;
import java.io.*;

public class Client implements Runnable
{
private Socket socket = null;
private Thread thread = null;
private DataInputStream userConsole = null;
private DataOutputStream outputStream = null;
private ClientThread client = null;


/*
Create client object
*/
public Client(String serverName, int serverPort)
{
System.out.println("Connecting to server host: " + serverName + " at port: " + serverPort);
try
{
socket = new Socket(serverName, serverPort);
System.out.println("Connected: " + socket);
start();
}
catch (UnknownHostException e)
{
System.out.println("Host unknown: " + e.getMessage());
}
catch (IOException e)
{
System.out.println("Unexpected exception: " + e.getMessage());
}
}

/*
create a new client thread
*/
public void run()
{
while (thread != null) {
try {
outputStream.writeUTF(userConsole.readLine());
outputStream.flush();
} catch (IOException e) {
System.out.println("Sending error: " + e.getMessage());
stop();
}
}
}


/*
method for a client to send a message
*/
public void sendMessage(String message)
{
if (message.equals("logmeout")) {
System.out.println("logmeout recognized. Press RETURN to exit.");
stop();
}
else
System.out.println(message);
}

/*
initialize a client thread
*/
public void start() throws IOException
{
userConsole = new DataInputStream(System.in);
outputStream = new DataOutputStream(socket.getOutputStream());

if (thread == null)
{
client = new ClientThread(this, socket);
thread = new Thread(this);
thread.start();
}
}

/*
Log out a client thread, shut down connections, close streams
*/
public void stop()
{
if (thread != null) {
thread.interrupt();
thread = null;
}

try
{
if (userConsole != null)
userConsole.close();
if (outputStream != null)
outputStream.close();
if (socket != null)
socket.close();
}
catch (IOException e)
{
System.out.println("Error closing client process. Error message: " + e);
}
client.close();
client.interrupt();
}

public static void main(String args[])
{
Client client = null;
if (args.length != 2)
System.out.println("Usage: java Client host port");
else
client = new Client(args[0], Integer.parseInt(args[1]));
}
}
Binary file added ClientServerFramework/ClientThread.class
Binary file not shown.
62 changes: 62 additions & 0 deletions ClientServerFramework/ClientThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Created by danielbardin on 4/6/15.
*/

import java.net.*;
import java.io.*;

public class ClientThread extends Thread
{

private Socket socket = null;
private Client client = null;
//private BufferedReader inputBuffer = null;
private DataInputStream inputStream = null;

public ClientThread(Client client, Socket socket)
{
this.client = client;
this.socket = socket;
open();
start();
}

/*
Connects this client via socket and creates a input reader
*/
public void open()
{
try {
inputStream = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
System.out.println("Error getting input stream: " + e);
client.stop();
}
}

public void close()
{
try {
if (inputStream != null)
inputStream.close();
}
catch (IOException e) {
System.out.println("Error closing input stream: " + e);
}
}

/*
Listener for client in infinite loop. Terminates when "logmeoff" read by server during message send
*/
public void run()
{
while (true) {
try {
client.sendMessage(inputStream.readUTF());
} catch (IOException e) {
System.out.println("Listening error: " + e.getMessage());
client.stop();
}
}
}
}
184 changes: 184 additions & 0 deletions ClientServerFramework/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/**
* Created by danielbardin on 4/6/15.
*/

import java.net.*;
import java.io.*;
import java.util.ArrayList;

/*
Class to implement the server-side functionality and main method for a chat service
*/
public class Server implements Runnable
{
// ArrayList of client/thread objects
private ArrayList<ServerThread> clients = new ArrayList<ServerThread>();

// This server's socket
private ServerSocket server = null;

// This server's main thread;
private Thread thread = null;

// How many clients are on this channel
private int numClients = 0;

// Maximum number of clients allowed on this server
private static final int MAX_CLIENTS = 25;


/*
Constructor
*/
public Server(int port)
{
try {
// Prompt user of port connection attempt
System.out.println("Trying to connect to port: " + port);

// make the connection to the port on our server
server = new ServerSocket(port);

// prompt user of connection success.
System.out.println("Connection successful.");

// start up the client
start();

} catch (IOException e) {
// Problem connecting
System.out.println("Could not attach to port: " + port + " The error was: " + e.getMessage());
}
}

/*
Start up
*/
public void run()
{
while (thread != null) {
try {
System.out.println("Waiting for first client connection. ");
addClient(server.accept());
} catch (IOException e) {
System.out.println("Server run() error: " + e);
}
}
}


/*
Activate client thread
*/
public void start()
{
if (thread == null) {
thread = new Thread(this);
thread.start();
}
}

// /*
// Terminate client thread
// */
// public void interrupt()
// {
// if (!thread.isInterrupted()) {
// thread.interrupt();
// thread = null;
// }
// }

/*
Find the client attempting to connect
*/
private int findClient(int id)
{
for (int i = 0; i < numClients; i++) {
if (clients.get(i).getId() == id)
return i;
}
return -1;
}


/*
Post a message to all users in this channel(i.e., host@port) from this id
*/
public synchronized void sendMessage(int id, String message)
{
// logmeout phrase will exit a person from the channel
if (message.equals("logmeout")) {
logoff(id);
}
else {
// Loops the clients array and posts the message to each.
for (ServerThread st : clients) {
st.sendMessage(id + ": " + message);
}
}
}


/*
Remove a user from the active roster and close their connection
*/
public synchronized void logoff(int id)
{
int clientsArrPos = findClient(id);

if (clients.get(clientsArrPos).getId() == id) {

numClients--;

try {
clients.get(clientsArrPos).close();
clients.remove(clientsArrPos);
}
catch (IOException e) {
System.out.println("Failed to log client off. Error message: " + e);
}
}
}


/*
Add a client thread to the server
*/
private void addClient(Socket socket)
{
// make sure clients is < 25 connections
if (numClients < MAX_CLIENTS) {
System.out.println("Client socket connection valid: " + socket);

// Add a client thread to clients roster of active users.
clients.add(new ServerThread(this, socket));

try {
clients.get(numClients).open();
clients.get(numClients).start();
numClients++;
}
catch (IOException e) {
System.out.println("Error opening client thread: " + e);
clients.remove(clients.size()-1);
}
}
else {
System.out.println("Sorry, this channel is full.");
}
}

/*
MAIN METHOD
*/
public static void main(String args[])
{
Server server = null;

if (args.length != 1)
System.out.println("Usage: java Server port");
else
server = new Server(Integer.parseInt(args[0]));
}
}
Loading