Advertisement
Guest User

ChatServer.java

a guest
May 1st, 2013
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import static java.lang.System.out;
  4. import java.net.*;
  5. import java.util.*;
  6.  
  7. public class ChatServer {
  8.    
  9.     Vector<String> users = new Vector<String>();
  10.     Vector<HandleClient> clients = new Vector<HandleClient>();
  11.  
  12.     public void process() throws Exception {
  13.         ServerSocket server = new ServerSocket(18524);
  14.         out.println("Server Started...");
  15.         while (true) {
  16.             Socket client = server.accept();
  17.             //add incoming client to connected clients vector.
  18.             HandleClient c = new HandleClient(client);
  19.             clients.add(c);
  20.         }  // end of while
  21.     }
  22.  
  23.     public static void main(String... args) throws Exception {
  24.         new ChatServer().process();
  25.     } // end of main
  26.  
  27.     public void broadcast(String user, String message) {
  28.         // send message to all connected users
  29.         for (HandleClient c : clients) {
  30.                 c.sendMessage(user, message);
  31.         }
  32.     }
  33.     /*
  34.      * Inner class, responsible of handling incoming clients.
  35.      * Each connected client will set as it's own thread.
  36.      */
  37.     class HandleClient extends Thread {
  38.  
  39.         String name = "";//client name/username
  40.         BufferedReader input;//get input from client
  41.         PrintWriter output;//send output to client
  42.  
  43.         public HandleClient(Socket client) throws Exception {
  44.             // get input and output streams
  45.             input = new BufferedReader(new InputStreamReader(client.getInputStream()));
  46.             output = new PrintWriter(client.getOutputStream(), true);
  47.             // read name
  48.             name = input.readLine();
  49.             users.add(name); // add to users vector
  50.             broadcast(name, " Has connected!");
  51.             start();
  52.         }
  53.  
  54.         public void sendMessage(String uname, String msg) {
  55.             output.println(uname + ": " + msg);
  56.         }
  57.        
  58.         public String getUserName() {
  59.             return name;
  60.         }
  61.        
  62.         public void run() {
  63.             String line;
  64.             try {
  65.                 while (true) {
  66.                     line = input.readLine();
  67.                     if (line.equals("end")) {
  68.                         //notify all for user disconnection
  69.                         broadcast(name, " Has disconnected!");
  70.                         clients.remove(this);
  71.                         users.remove(name);
  72.                         break;
  73.                     }
  74.                     broadcast(name, line); // method  of outer class - send messages to all
  75.                 } // end of while
  76.             } // try
  77.             catch (Exception ex) {
  78.                 System.out.println(ex.getMessage());
  79.             }
  80.         } // end of run()
  81.     } // end of inner class
  82. } // end of Server
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement