Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 3.55 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.util.*;
  2. import java.net.*;
  3. import java.io.*;
  4.  
  5. //appletviewer -J"-Djava.security.policy=all.policy" chat.html
  6. class ClientBuffer
  7. {
  8.         private static ArrayList<String> clients = new ArrayList<String>();
  9.         private static ArrayList<PrintWriter> clientInput = new ArrayList<PrintWriter>();
  10.  
  11.         private Socket socket = null; // The socket passed from the creator
  12.         //private boolean loop = true;
  13.         private String id = null;
  14.     PrintWriter socketOut = null; // We use a PrintWriter to write to the socket
  15.         BufferedReader socketIn = null;// We use a BufferedReader to read from the socket
  16.         String userInput;// We store user input in a string
  17.  
  18.         public ClientBuffer(Socket socket0) {
  19.  
  20.         socket = socket0;
  21.  
  22.     }
  23.         public int numberOfClients()
  24.         {
  25.                 return clients.size();
  26.         }
  27.  
  28.         public synchronized void getClients()
  29.         {
  30.                 boolean getClientsLoop = true;
  31.                 try
  32.                 {
  33.                         while(getClientsLoop)
  34.                         {
  35.  
  36.                                 id = socketIn.readLine();
  37.  
  38.                                
  39.                                 if(!clients.contains(id))
  40.                                 {
  41.                                                 clients.add(id);
  42.                                                 //t.println(id + " has just joined chat room...");
  43.                                                 getClientsLoop = false;
  44.                                 }                                              
  45.                         }
  46.                 }
  47.                 catch(IOException e)
  48.                 {
  49.                 }
  50.         }
  51.  
  52.         public synchronized void getClientInput()
  53.         {
  54.                 clientInput.add(socketOut);
  55.  
  56.                 for(int i = 0; i < clientInput.size(); i++)
  57.                 {
  58.                         PrintWriter t = clientInput.get(i);
  59.  
  60.                         t.println(id + " has just joined chat room...");
  61.                 }
  62.         }
  63.  
  64.         public synchronized void printClientInput()
  65.         {
  66.                 boolean printInputLoop = true;
  67.                 try
  68.                 {      
  69.                         while(printInputLoop){
  70.                                        
  71.                                         userInput = socketIn.readLine();
  72.  
  73.                                         for(int i = 0; i < clientInput.size(); i++){
  74.                                                 PrintWriter t = clientInput.get(i);
  75.  
  76.                                                 t.println(id + ": " + userInput);
  77.                                         }
  78.                                 }
  79.                 }
  80.                 catch(IOException e)
  81.                 {
  82.                 }
  83.         }
  84.  
  85.         public synchronized void removeClient()
  86.         {
  87.                 //removes name from array list
  88.                 clients.remove(clients.indexOf(id));
  89.  
  90.                 //print new size of clients
  91.                 System.out.println("Clients: " + clients.size());
  92.  
  93.                 //tell other users of the client leaving
  94.                 for(int i = 0; i < clientInput.size(); i++){
  95.                         PrintWriter t = clientInput.get(i);
  96.  
  97.                         t.println(id + " has left the chatroom...");
  98.                 }
  99.  
  100.                 if(socketOut != null){
  101.                         clientInput.remove(socketOut);
  102.                 }
  103.         }
  104. }
  105.  
  106. class Consumer extends Thread
  107. {
  108.         private ClientBuffer cb = new ClientBuffer();
  109.         private int rand;
  110.  
  111.         Consumer(ClientBuffer cb0)
  112.         {
  113.                 cb = cb0;
  114.         }
  115.  
  116.         public void run()
  117.         {
  118.                 cb.getClients();
  119.                 cb.getClientInput();
  120.  
  121.                 rand = (int)((Math.random()) * 101);
  122.  
  123.                 try
  124.                 {
  125.                         Consumer.sleep(rand);
  126.                 }
  127.                 catch (InterruptedException e)
  128.                 {
  129.                 }
  130.         }
  131.  
  132.  
  133.  
  134. }
  135.  
  136. class Sender extends Thread
  137. {
  138.         private ClientBuffer cb = new ClientBuffer();
  139.         private int rand;
  140.  
  141.         Sender(ClientBuffer cb0)
  142.         {
  143.                 cb = cb0;
  144.         }
  145.  
  146.         public void run()
  147.         {
  148.                 cb.printClientInput();
  149.                 cb.removeClient();
  150.  
  151.                 rand = (int)((Math.random()) * 101);
  152.  
  153.                 try
  154.                 {
  155.                         Sender.sleep(rand);
  156.                 }
  157.                 catch (InterruptedException e)
  158.                 {
  159.                 }
  160.         }
  161.  
  162. }
  163.  
  164. class ChatServer{
  165.  
  166.         public static void main(String [] args){
  167.                
  168.                 ServerSocket ss = null;
  169.                 boolean online = true;
  170.  
  171.                 try{
  172.                         ss = new ServerSocket(7777);
  173.  
  174.                         while(online){
  175.                                 ClientBuffer cb = new ClientBuffer(ss.accept());
  176.                                 Thread Consumer0 = new Consumer(cb);
  177.                                 Thread Sender0 = new Sender(cb);
  178.  
  179.                                 Consumer0.start();
  180.                                 Sender0.start();
  181.                                 System.out.println("Clients: " + cb.numberOfClients());
  182.                         }
  183.                        
  184.                 }catch(IOException e){
  185.                         System.err.println("could not listen on port: 7777");
  186.                         System.exit(-1);
  187.                 }
  188.  
  189.                 try{
  190.                         ss.close();
  191.                 }catch (Exception e){
  192.                         System.out.println(e);
  193.                 }
  194.         }
  195. }