Server.java

Server.java · 5.0 KB

From the archive Chatter.zip

Download the archive   « Back to the archive contents

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

public class Server
{
	static Server server;
	ConnectionListener connectionListener;
	ConsoleListener consoleListener;
	
	ArrayList<User> users;
	ServerSocket serverSocket;
	
	Scanner commandScanner;
	String commandString;
	
	public static void main(String args[])
	{
		server = new Server();
		server.start();
	}
	
	public Server()
	{
		try
		{
			serverSocket = new ServerSocket(4444);
		}
		catch(Exception e)
		{
			System.out.print("This port is in use.");
		}
	}
	
	public void start()
	{
		connectionListener = new ConnectionListener(this);
		consoleListener = new ConsoleListener(this);
		
		connectionListener.start();
		consoleListener.start();
	}
	
	public void consoleCommand(String command)
	{
		commandScanner = new Scanner(command);
		
		if(commandScanner.hasNext())
		{
			commandString = commandScanner.next();
			
			if(commandString.equalsIgnoreCase("quit") || commandString.equalsIgnoreCase("q") || commandString.equalsIgnoreCase("exit") || commandString.equalsIgnoreCase("x") || commandString.equalsIgnoreCase("leave"))
			{
				try
				{
					if(users != null)
					{
						for(int i = 0; i < users.size(); i++)
						{
							users.get(i).send("server end", "");
							users.get(i).userSocket.close();
						}
					}
					
					System.out.println(Utility.getTimestamp() + "Ending server.");
					System.out.println(Utility.getTimestamp() + "Goodbye!");
					System.exit(0);
				}
				catch(Exception e)
				{
					System.out.println(Utility.getTimestamp() + "Server shutdown aborted.");
				}
			}
			
			else if(commandString.equalsIgnoreCase("send"))
			{
				String sendString = commandScanner.nextLine();
				
				for(int i = 0; i < users.size(); i++)
				{
					users.get(i).send("send", sendString);
				}
			}
		}
	}
	
	public void userCommand(String command)
	{
		commandScanner = new Scanner(command);
		
		if(commandScanner.hasNext())
		{
			commandString = commandScanner.next();
			
			if(commandString.equalsIgnoreCase("send"))
			{
				String sendString = commandScanner.nextLine();
				
				for(int i = 0; i < users.size(); i++)
				{
					users.get(i).send("send", sendString);
				}
			}
			
			else if(commandString.equalsIgnoreCase("connectionclosing"))
			{
			}
		}
	}
}

class ConsoleListener extends Thread
{
	Server consoleListenerServer;
	
	BufferedReader input;
	String inputString;
	
	public ConsoleListener(Server passedServer)
	{
		consoleListenerServer = passedServer;
		input = new BufferedReader(new InputStreamReader(System.in));
	}
	
	public void run()
	{
		while(true)
		{
			try
			{
				System.out.print(Utility.getTimestamp());
				
				while((inputString = input.readLine()) != null)
				{
					consoleListenerServer.consoleCommand(inputString);
					
					System.out.print(Utility.getTimestamp());
				}
			}
			catch(Exception e)
			{
				System.out.println(Utility.getTimestamp() + "Input failed.");
			}
		}
	}
}

class ConnectionListener extends Thread
{
	Server connectionListenerServer;
	Socket connectionListenerSocket;
	User newUser;
	
	public ConnectionListener(Server passedServer)
	{
		connectionListenerServer = passedServer;
		connectionListenerServer.users = new ArrayList();
	}
	
	public void run()
	{		
		while(true)
		{
			try
			{
				connectionListenerSocket = connectionListenerServer.serverSocket.accept();
			}
			catch(Exception e)
			{
				continue;
			}
			
			System.out.println("Connected: " + connectionListenerSocket);
			System.out.print(Utility.getTimestamp());
			
			try
			{		
				newUser = new User(connectionListenerServer, connectionListenerSocket);
				connectionListenerServer.users.add(newUser);
				connectionListenerServer.users.get(connectionListenerServer.users.size() - 1).start();
			}
			catch(Exception e)
			{
				System.out.println("ConnectionListener thread error.");
				System.out.print(Utility.getTimestamp());
			}
		}
	}
}

class User extends Thread
{
	Server userServer;
	Socket userSocket;
	
	PrintWriter output;
	BufferedReader input;
	String outputString, inputString;
	
	String userName;
	
	public User(Server passedServer, Socket passedSocket)
	{
		userServer = passedServer;
		userSocket = passedSocket;
		
		try
		{
			output = new PrintWriter(userSocket.getOutputStream(), true);
			input = new BufferedReader(new InputStreamReader(userSocket.getInputStream()));
		}
		catch(Exception e)
		{
		}
		
		outputString = inputString = userName = "";
	}
	
	public void run()
	{
		try
		{
			while((inputString = input.readLine()) != null)
			{
				System.out.println("Incoming message: " + inputString);
				System.out.print(Utility.getTimestamp());
				userServer.userCommand(inputString);
			}
		}
		catch(Exception e)
		{
		}
	}
	
	public void send(String command, String message)
	{
		output.println(command + " " + userName + " " + message);
	}
}

Site Stats

Snippets: 1791
Codebases: 571