Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import static java.lang.System.out;
- import java.net.*;
- import java.util.*;
- public class ChatServer {
- Vector<String> users = new Vector<String>();
- Vector<HandleClient> clients = new Vector<HandleClient>();
- public void process() throws Exception {
- ServerSocket server = new ServerSocket(18524);
- out.println("Server Started...");
- while (true) {
- Socket client = server.accept();
- //add incoming client to connected clients vector.
- HandleClient c = new HandleClient(client);
- clients.add(c);
- } // end of while
- }
- public static void main(String... args) throws Exception {
- new ChatServer().process();
- } // end of main
- public void broadcast(String user, String message) {
- // send message to all connected users
- for (HandleClient c : clients) {
- c.sendMessage(user, message);
- }
- }
- /*
- * Inner class, responsible of handling incoming clients.
- * Each connected client will set as it's own thread.
- */
- class HandleClient extends Thread {
- String name = "";//client name/username
- BufferedReader input;//get input from client
- PrintWriter output;//send output to client
- public HandleClient(Socket client) throws Exception {
- // get input and output streams
- input = new BufferedReader(new InputStreamReader(client.getInputStream()));
- output = new PrintWriter(client.getOutputStream(), true);
- // read name
- name = input.readLine();
- users.add(name); // add to users vector
- broadcast(name, " Has connected!");
- start();
- }
- public void sendMessage(String uname, String msg) {
- output.println(uname + ": " + msg);
- }
- public String getUserName() {
- return name;
- }
- public void run() {
- String line;
- try {
- while (true) {
- line = input.readLine();
- if (line.equals("end")) {
- //notify all for user disconnection
- broadcast(name, " Has disconnected!");
- clients.remove(this);
- users.remove(name);
- break;
- }
- broadcast(name, line); // method of outer class - send messages to all
- } // end of while
- } // try
- catch (Exception ex) {
- System.out.println(ex.getMessage());
- }
- } // end of run()
- } // end of inner class
- } // end of Server
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement