- import java.util.*;
- import java.net.*;
- import java.io.*;
- //appletviewer -J"-Djava.security.policy=all.policy" chat.html
- class ClientBuffer
- {
- private static ArrayList<String> clients = new ArrayList<String>();
- private static ArrayList<PrintWriter> clientInput = new ArrayList<PrintWriter>();
- private Socket socket = null; // The socket passed from the creator
- //private boolean loop = true;
- private String id = null;
- PrintWriter socketOut = null; // We use a PrintWriter to write to the socket
- BufferedReader socketIn = null;// We use a BufferedReader to read from the socket
- String userInput;// We store user input in a string
- public ClientBuffer(Socket socket0) {
- socket = socket0;
- }
- public int numberOfClients()
- {
- return clients.size();
- }
- public synchronized void getClients()
- {
- boolean getClientsLoop = true;
- try
- {
- while(getClientsLoop)
- {
- id = socketIn.readLine();
- if(!clients.contains(id))
- {
- clients.add(id);
- //t.println(id + " has just joined chat room...");
- getClientsLoop = false;
- }
- }
- }
- catch(IOException e)
- {
- }
- }
- public synchronized void getClientInput()
- {
- clientInput.add(socketOut);
- for(int i = 0; i < clientInput.size(); i++)
- {
- PrintWriter t = clientInput.get(i);
- t.println(id + " has just joined chat room...");
- }
- }
- public synchronized void printClientInput()
- {
- boolean printInputLoop = true;
- try
- {
- while(printInputLoop){
- userInput = socketIn.readLine();
- for(int i = 0; i < clientInput.size(); i++){
- PrintWriter t = clientInput.get(i);
- t.println(id + ": " + userInput);
- }
- }
- }
- catch(IOException e)
- {
- }
- }
- public synchronized void removeClient()
- {
- //removes name from array list
- clients.remove(clients.indexOf(id));
- //print new size of clients
- System.out.println("Clients: " + clients.size());
- //tell other users of the client leaving
- for(int i = 0; i < clientInput.size(); i++){
- PrintWriter t = clientInput.get(i);
- t.println(id + " has left the chatroom...");
- }
- if(socketOut != null){
- clientInput.remove(socketOut);
- }
- }
- }
- class Consumer extends Thread
- {
- private ClientBuffer cb = new ClientBuffer();
- private int rand;
- Consumer(ClientBuffer cb0)
- {
- cb = cb0;
- }
- public void run()
- {
- cb.getClients();
- cb.getClientInput();
- rand = (int)((Math.random()) * 101);
- try
- {
- Consumer.sleep(rand);
- }
- catch (InterruptedException e)
- {
- }
- }
- }
- class Sender extends Thread
- {
- private ClientBuffer cb = new ClientBuffer();
- private int rand;
- Sender(ClientBuffer cb0)
- {
- cb = cb0;
- }
- public void run()
- {
- cb.printClientInput();
- cb.removeClient();
- rand = (int)((Math.random()) * 101);
- try
- {
- Sender.sleep(rand);
- }
- catch (InterruptedException e)
- {
- }
- }
- }
- class ChatServer{
- public static void main(String [] args){
- ServerSocket ss = null;
- boolean online = true;
- try{
- ss = new ServerSocket(7777);
- while(online){
- ClientBuffer cb = new ClientBuffer(ss.accept());
- Thread Consumer0 = new Consumer(cb);
- Thread Sender0 = new Sender(cb);
- Consumer0.start();
- Sender0.start();
- System.out.println("Clients: " + cb.numberOfClients());
- }
- }catch(IOException e){
- System.err.println("could not listen on port: 7777");
- System.exit(-1);
- }
- try{
- ss.close();
- }catch (Exception e){
- System.out.println(e);
- }
- }
- }