import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.*; import java.io.*; class Thread1 extends Thread { mailbox box; Thread1(mailbox box){ this.box = box; } public void run(){ int i = 0; ServerSocket socket = null; int port = 25005; try { socket = new ServerSocket(port); } catch (IOException e) { System.out.println("Could not create socket!"); System.exit(1); } client[] clients = new client[10]; ThreadClear clear = new ThreadClear(box, clients); clear.start(); while(true){ Socket clientSocket = null; try{ clientSocket = socket.accept(); }catch (IOException e) { System.out.println("Could not accept socket!"); System.exit(1); } InetAddress clientInet = clientSocket.getInetAddress(); System.out.println(new String(clientInet.toString())); clients[i] = new client(i); clients[i].startConnection(clientSocket,box); clear.setNbrOfClients(i); i++; } } } class clientThread extends Thread{ InputStreamReader inR; InputStream in = null; OutputStream out = null; BufferedReader br = null; mailbox box; Socket socket; int id; clientThread(Socket clientSocket,mailbox box,int id){ this.id = id; this.box = box; this.socket = clientSocket; try{ in = clientSocket.getInputStream(); inR = new InputStreamReader ( in ); br = new BufferedReader(inR); out = clientSocket.getOutputStream(); } catch (Exception e) { System.err.println(e); } } public void run(){ String s = ""; while(true){ s = ""; try{ s = s+br.readLine() ; }catch(IOException e){ box.assign(socket.getInetAddress()+ "has disconnected", -1); System.out.println(socket.getInetAddress()+ "has disconnected"); break; //System.exit(1); } synchronized(box){ while(!box.isEmpty()){ try{ box.wait(); }catch (InterruptedException e){ System.exit(1); } } box.assign(s,id); box.notifyAll(); } } } public void send(String s){ try{ out.write(s.getBytes()); }catch (Exception e) { System.err.println(e); System.exit(1); } } } class client{ clientThread thirdThread; int id; Socket socket; client(int id){ this.id = id; } public void startConnection(Socket clientSocket, mailbox box){ this.socket = clientSocket; this.thirdThread = new clientThread(clientSocket,box,id); thirdThread.start(); } public void send(String s){ thirdThread.send(s); } public void kill(){ try { this.socket.close(); } catch (IOException e) { System.out.println("Could not close socket"); System.exit(1); } } } class mailbox { String message; int id = 0; mailbox(String message){ this.message = message; } public void assign(String s, int id){ this.message = s; this.id = id; } public boolean isEmpty(){ if(message.equals("")){ return true; } return false; } public String erase(){ String s = message; message =""; return s; } public int getId(){ return this.id; } } class ThreadClear extends Thread{ mailbox box; client[] clients; int nbrOfClients = 0; ThreadClear(mailbox box, client[] clients){ this.box = box; this.clients = clients; } public void setNbrOfClients(int nbrOfClients){ this.nbrOfClients = nbrOfClients; } public void run(){ String s; synchronized(box){ while(true){ s = ""; while(box.isEmpty()){ A try{ box.wait(); }catch (InterruptedException e){ System.exit(1); } } int id = box.getId(); s = box.erase(); if(s.charAt(0)=='Q'){ clients[id].kill(); clients[id] = null; } if(s.charAt(0)=='M'){ for(int i = 0; i<=nbrOfClients;i++){ if(id!=i && clients[i] != null){ clients[i].send(s); } } } System.out.println(s); box.notifyAll(); } } } } public class chatS{ public static void main(String args[]) { mailbox box = new mailbox(""); Thread1 firstThread = new Thread1(box); firstThread.start(); } }