Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Server.java
- package chat;
- import java.io.*;
- import java.util.*;
- import java.net.*;
- import static java.lang.System.out;
- public class Server {
- ArrayList<String> users = new ArrayList<String>();
- ArrayList<HandleClient> clients = new ArrayList<HandleClient>();
- int PORT = 3333;
- int NumClients = 10;
- public Server() throws Exception {
- ServerSocket server = new ServerSocket(PORT, NumClients);
- out.println("Server Connected...");
- while (true) {
- Socket client = server.accept();
- HandleClient handleClient = new HandleClient(client);
- clients.add(handleClient);
- } // end of while
- }
- public void boradcast(String user, String message) {
- // send message to all connected users
- for (HandleClient handleClient : clients) { // clients.size() => number of clients
- if (!handleClient.getUserName().equals(user)) {
- handleClient.sendMessage(user, message);
- }
- }
- }
- public static void main(String[] args) throws Exception {
- new Server();
- } // end of main
- class HandleClient extends Thread { // beginning of inner class
- String loginName = "";
- BufferedReader fromClients;
- PrintWriter toClients;
- public HandleClient(Socket client) throws Exception {
- // get fromClients and toClients streams
- fromClients = new BufferedReader(new InputStreamReader(client.getInputStream()));
- toClients = new PrintWriter(client.getOutputStream(), true);
- // read loginName
- toClients.println("Please Enter a User Name: ");
- loginName = fromClients.readLine();
- users.add(loginName); // add to ArrayList
- toClients.println("Welcome " + loginName);
- toClients.println("Write \"EXIT\" to exit, and write your login name for your identification.\nLet's talk");
- start();
- }
- public void sendMessage(String uname, String msg) {
- toClients.println(uname + ":" + msg);
- }
- public String getUserName() {
- return loginName;
- }
- public void run() {
- String messageFromClient;
- try {
- while (true) {
- messageFromClient = fromClients.readLine();
- if ("EXIT".equals(messageFromClient)) {
- toClients.println("Closing Connection . . . Goodbye");
- clients.remove(this);
- users.remove(loginName);
- break;
- } else if (loginName.equals(messageFromClient)) {
- toClients.println("OK");
- } else {
- boradcast(loginName, messageFromClient);
- }// method of outer class - send messages to all
- }// end of while
- } // try
- catch (Exception e) {
- System.out.println(e.getMessage());
- }
- } // end of run()
- }
- } // end of Server
- // Client.java
- package chat;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- public class Client {
- String host = "localhost";
- int port = 3333;
- private Socket socket;
- private PrintWriter out;
- private Scanner scanner;
- private InputStreamReader in;
- public Client() throws IOException {
- socket = new Socket(host, port);
- in = new InputStreamReader(socket.getInputStream());
- scanner = new Scanner(in);
- out = new PrintWriter(socket.getOutputStream());
- new Thread(new Runnable() {
- @Override
- public void run() {
- String receivingMessage;
- try {
- while (true) {
- receivingMessage = scanner.nextLine();
- System.out.println(receivingMessage);
- }
- } catch (Exception e) {
- }
- }
- }).start();
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- while (true) {
- Scanner scn = new Scanner(System.in);
- String input = scn.nextLine();
- out.println(input);
- out.flush();
- }
- } catch (Exception e) {
- }
- }
- }).start();
- }
- public static void main(String[] args) throws IOException {
- new Client();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment