A simple java chat room and a simple java chat room
Using the group chat room compiled by java Socket, you can copy it and try it on your own
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 voi D 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 + "online");} 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 + "offline") ;}@ Overridepublic 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 () {Buffer EdReader 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 ("laolin"); // 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. repeated signorecase ("disconnect") {flag = false; System. out. println ("press enter to exit");} System. out. println (rs);} catch (IOException e) {e. printStackTrace () ;}} private class ClientThread implements Runnable {@ Overridepublic void run () {while (true) {if (! Flag) break; receive ();}}}}