Socket
ChatServer:
1 import java.io.BufferedReader; 2 import java.io.IOException; 3 import java.io.InputStreamReader; 4 import java.io.PrintWriter; 5 import java.net.ServerSocket; 6 import java.net.Socket; 7 import java.util.ArrayList; 8 import java.util.List; 9 import java.util.concurrent.ExecutorService; 10 import java.util.concurrent.Executors; 11 12 public class ChatServer 13 { 14 private static final int PORT = 8080; 15 private static List<Socket> list; 16 private ExecutorService exec; 17 private ServerSocket server; 18 19 public static void main(String[] args) 20 { 21 new ChatServer().start(); 22 } 23 24 public ChatServer() 25 { 26 list = new ArrayList<Socket>(); 27 exec = Executors.newCachedThreadPool(); 28 try 29 { 30 server = new ServerSocket(PORT); 31 } 32 catch (IOException e) 33 { 34 e.printStackTrace(); 35 } 36 } 37 38 public void start() 39 { 40 System.out.println("The server has started."); 41 Socket client = null; 42 while (true) 43 { 44 try 45 { 46 client = server.accept(); 47 list.add(client); 48 exec.execute(new ChatTask(client)); 49 } 50 catch (IOException e) 51 { 52 e.printStackTrace(); 53 } 54 } 55 } 56 57 static class ChatTask implements Runnable 58 { 59 private Socket socket; 60 private BufferedReader br; 61 private PrintWriter pw; 62 private String msg; 63 64 public ChatTask(Socket socket) throws IOException 65 { 66 this.socket = socket; 67 br = new BufferedReader(new InputStreamReader(socket.getInputStream())); 68 msg = "[" + this.socket.getInetAddress() + "]enter the chat room!There are ["+ list.size() + "] people."; 69 sendMessage(); 70 } 71 72 public void run() 73 { 74 try 75 { 76 while ((msg = br.readLine()) != null) 77 { 78 if (msg.trim().equals("bye")) 79 { 80 list.remove(socket); 81 br.close(); 82 pw.close(); 83 msg = "[" + socket.getInetAddress() + "] leave chat room!There are [" + list.size() + "] people."; 84 socket.close(); 85 sendMessage(); 86 break; 87 } 88 else 89 { 90 msg = "[" + socket.getInetAddress() + "] says:" + msg; 91 sendMessage(); 92 } 93 } 94 } 95 catch (IOException e) 96 { 97 e.printStackTrace(); 98 } 99 } 100 101 private void sendMessage() throws IOException 102 { 103 System.out.println(msg); 104 for (Socket client : list) 105 { 106 pw = new PrintWriter(client.getOutputStream(), true); 107 pw.println(msg); 108 } 109 } 110 } 111 }
CharClient:
1 import java.io.BufferedReader; 2 import java.io.InputStreamReader; 3 import java.io.PrintWriter; 4 import java.net.Socket; 5 import java.util.concurrent.ExecutorService; 6 import java.util.concurrent.Executors; 7 8 public class ChatClient 9 { 10 private static final int PORT = 8080; 11 private static ExecutorService exec; 12 13 public static void main(String[] args) throws Exception 14 { 15 new ChatClient().start(); 16 } 17 18 public ChatClient() 19 { 20 exec = Executors.newCachedThreadPool(); 21 } 22 23 public void start() 24 { 25 try 26 { 27 Socket socket = new Socket("127.0.0.1", PORT); 28 exec.execute(new Sender(socket)); 29 System.out.println("[" + socket.getInetAddress() + "] Welcome to the chat room!"); 30 BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream())); 31 String msg = null; 32 while ((msg = br.readLine()) != null) 33 { 34 System.out.println(msg); 35 } 36 } 37 catch (Exception e) 38 { 39 e.printStackTrace(); 40 } 41 } 42 43 static class Sender implements Runnable 44 { 45 private Socket socket; 46 47 public Sender(Socket socket) 48 { 49 this.socket = socket; 50 } 51 52 public void run() 53 { 54 try 55 { 56 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 57 PrintWriter pw = new PrintWriter(socket.getOutputStream(), true); 58 String msg = null; 59 while (true) 60 { 61 msg = br.readLine(); 62 pw.println(msg); 63 if (msg.trim().equals("bye")) 64 { 65 pw.close(); 66 br.close(); 67 exec.shutdownNow(); 68 break; 69 } 70 } 71 } 72 catch (Exception e) 73 { 74 e.printStackTrace(); 75 } 76 } 77 } 78 }