利用java Socket编写的群聊室,可以自己拷过去试试
Server端:
package net3; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.ArrayList; import java.util.List; public class Server { private List<ServerThread> clients = null; public static void main(String[] args) { new Server().startUp(); } private void startUp() { ServerSocket ss = null; Socket s = null; try { ss = new ServerSocket(5858); clients = new ArrayList<ServerThread>(); while (true) { s = ss.accept(); ServerThread st = new ServerThread(s); new Thread(st).start(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (ss != null) ss.close(); } catch (IOException e) { e.printStackTrace(); } } } private class ServerThread implements Runnable { private Socket s = null; private BufferedReader br; private PrintWriter out; private String name; private boolean flag = true; public ServerThread(Socket socket) throws IOException { this.s = socket; br = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream(), true); String str = br.readLine(); name = str+"["+socket.getInetAddress().getHostAddress()+":"+socket.getPort()+"]"; clients.add(this); send(name+"上线了"); } private void send(String msg) { for (ServerThread st : clients) st.out.println(msg); } private void receive() throws IOException { String str = null; while ((str=br.readLine()) != null) { if (str.equalsIgnoreCase("quit")) { stop(); out.println("disconnect"); break; } send(name+":"+str); } } private void stop() { clients.remove(this); flag = false; send(name+"已经下线了"); } @Override public void run() { try { while (true) { if (!flag) break; receive(); } } catch (SocketException e) { stop(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (s != null) s.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
Client端
package net3; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import java.net.UnknownHostException; public class Client { private Socket s; private BufferedReader br; private PrintWriter out; private boolean flag = true; public static void main(String[] args) { new Client().stratUp(); } private void stratUp() { BufferedReader sbr = null; try { s = new Socket("127.0.0.1", 5858); out = new PrintWriter(s.getOutputStream(), true); br = new BufferedReader(new InputStreamReader(s.getInputStream())); out.println("老林"); // out.println("老周"); sbr = new BufferedReader(new InputStreamReader(System.in)); new Thread(new ClientThread()).start(); String str = null; while (flag && (str=sbr.readLine())!=null) { if (!flag) break; out.println(str); } } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (s != null) s.close(); } catch (IOException e) { e.printStackTrace(); } try { if (sbr != null) s.close(); } catch (IOException e) { e.printStackTrace(); } } } private void receive() { try { String rs = br.readLine(); if (rs.equalsIgnoreCase("disconnect")) { flag = false; System.out.println("点击回车退出"); } System.out.println(rs); } catch (IOException e) { e.printStackTrace(); } } private class ClientThread implements Runnable { @Override public void run() { while (true) { if (!flag) break; receive(); } } } }