aquaballoon

Java - Chat

May 27th, 2014
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1. // Server.java
  2. package chat;
  3.  
  4. import java.io.*;
  5. import java.util.*;
  6. import java.net.*;
  7. import static java.lang.System.out;
  8.  
  9. public class Server {
  10.  
  11.     ArrayList<String> users = new ArrayList<String>();
  12.     ArrayList<HandleClient> clients = new ArrayList<HandleClient>();
  13.  
  14.     int PORT = 3333;
  15.     int NumClients = 10;
  16.  
  17.     public Server() throws Exception {
  18.         ServerSocket server = new ServerSocket(PORT, NumClients);
  19.         out.println("Server Connected...");
  20.         while (true) {
  21.             Socket client = server.accept();
  22.             HandleClient handleClient = new HandleClient(client);
  23.             clients.add(handleClient);
  24.         }  // end of while
  25.     }
  26.  
  27.     public void boradcast(String user, String message) {
  28.         // send message to all connected users
  29.         for (HandleClient handleClient : clients) {  // clients.size() => number of clients
  30.             if (!handleClient.getUserName().equals(user)) {
  31.                 handleClient.sendMessage(user, message);
  32.             }
  33.         }
  34.     }
  35.    
  36.     public static void main(String[] args) throws Exception {
  37.         new Server();
  38.     } // end of main
  39.  
  40.     class HandleClient extends Thread {  // beginning of inner class
  41.  
  42.         String loginName = "";
  43.         BufferedReader fromClients;
  44.         PrintWriter toClients;
  45.  
  46.         public HandleClient(Socket client) throws Exception {
  47.             // get fromClients and toClients streams
  48.             fromClients = new BufferedReader(new InputStreamReader(client.getInputStream()));
  49.             toClients = new PrintWriter(client.getOutputStream(), true);
  50.             // read loginName
  51.             toClients.println("Please Enter a User Name: ");
  52.             loginName = fromClients.readLine();
  53.             users.add(loginName); // add to ArrayList
  54.             toClients.println("Welcome " + loginName);
  55.             toClients.println("Write \"EXIT\" to exit, and write your login name for your identification.\nLet's talk");
  56.             start();
  57.         }
  58.  
  59.         public void sendMessage(String uname, String msg) {
  60.             toClients.println(uname + ":" + msg);
  61.         }
  62.  
  63.         public String getUserName() {
  64.             return loginName;
  65.         }
  66.  
  67.         public void run() {
  68.             String messageFromClient;
  69.             try {
  70.                 while (true) {
  71.                     messageFromClient = fromClients.readLine();
  72.                     if ("EXIT".equals(messageFromClient)) {
  73.                         toClients.println("Closing Connection  . . . Goodbye");
  74.                         clients.remove(this);
  75.                         users.remove(loginName);
  76.                         break;
  77.                     } else if (loginName.equals(messageFromClient)) {
  78.                         toClients.println("OK");
  79.                     } else {
  80.                         boradcast(loginName, messageFromClient);
  81.                     }// method  of outer class - send messages to all
  82.                 }// end of while
  83.             } // try
  84.             catch (Exception e) {
  85.                 System.out.println(e.getMessage());
  86.             }
  87.         } // end of run()
  88.     }
  89. } // end of Server
  90.  
  91.  
  92. // Client.java
  93. package chat;
  94.  
  95. import java.io.*;
  96. import java.net.*;
  97. import java.util.*;
  98.  
  99. public class Client {
  100.  
  101.     String host = "localhost";
  102.     int port = 3333;
  103.     private Socket socket;
  104.     private PrintWriter out;
  105.     private Scanner scanner;
  106.     private InputStreamReader in;
  107.  
  108.     public Client() throws IOException {
  109.         socket = new Socket(host, port);
  110.         in = new InputStreamReader(socket.getInputStream());
  111.         scanner = new Scanner(in);
  112.         out = new PrintWriter(socket.getOutputStream());
  113.  
  114.         new Thread(new Runnable() {
  115.             @Override
  116.             public void run() {
  117.                 String receivingMessage;
  118.                 try {
  119.                     while (true) {
  120.                         receivingMessage = scanner.nextLine();
  121.                         System.out.println(receivingMessage);
  122.                     }
  123.                 } catch (Exception e) {
  124.  
  125.                 }
  126.             }
  127.         }).start();
  128.  
  129.         new Thread(new Runnable() {
  130.             @Override
  131.             public void run() {
  132.                 try {
  133.                     while (true) {
  134.                         Scanner scn = new Scanner(System.in);
  135.                         String input = scn.nextLine();
  136.                         out.println(input);
  137.                         out.flush();
  138.                     }
  139.                 } catch (Exception e) {
  140.  
  141.                 }
  142.             }
  143.         }).start();
  144.  
  145.     }
  146.  
  147.     public static void main(String[] args) throws IOException {
  148.         new Client();
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment