A group chat room written using a Java socket, you can try to copy the past
Server side:
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.printstac Ktrace ();}}} 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+ "on-line");} 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+ "already offline");} @Overridepublic void Run () {try {while (true) {if (!flag) break;receive ()}} catch (SocketException e) {Stop ();} catch (IO Exception e) {e.printstacktrace ();} finally {try {if (s! = null) S.close ();} catch (IOException e) {e.printstacktrace ();} }}}}
Client Side
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 ;p rivate 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.getoutput Stream (), true), br = new BufferedReader (New InputStreamReader (S.getinputstream ())), Out.println ("Lim");//out.println ( "Old Week"); 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 ("Click 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 ();}}}}
A simple Java chat room