This example for you to share the Java implementation of Tcpsocket chat room function of the relevant code for your reference, the specific content as follows
1.tcpserver.java
Import java.net.*;
Import java.io.*;
Import java.util.*;
Import java.util.concurrent.*;
public class tcpserver{private static final int serverport = 8888;
Private ServerSocket MyServer = null;
Private list<socket> Clients = new arraylist<socket> ();
Private Executorservice Mexecutorservice;
Public TCPServer () {try{MyServer = new ServerSocket (serverport);
Mexecutorservice = Executors.newcachedthreadpool ();
System.out.println ("Start:");
Socket mysocket = null;
while (true) {mysocket = Myserver.accept ();
Clients.add (Mysocket);
Mexecutorservice.execute (New Threadserver (Mysocket));
}}catch (Exception e) {e.printstacktrace ();
System.exit (0);
} class Threadserver implements runnable{private Socket msocket=null;
Private BufferedReader in= null;
Private printwriter out = null;
Private String mstrmsg = null; Public threadserver (socket socket) {try{
This.msocket=socket;
in = new BufferedReader (New InputStreamReader (Msocket.getinputstream (), "GB2312"));
mstrmsg = "User:" + msocket.getinetaddress () + "Come total:" + clients.size ();
Sendmassage ();
}catch (IOException e) {System.out.println ("Erorr");
System.exit (0);
}} private void Sendmassage () {try{System.out.println (mstrmsg); for (Socket mysocket:clients) {out = new PrintWriter (New OutputStreamWriter (Mysocket.getoutputstream (), GB
2312 "), true);
Out.println (MSTRMSG);
}}catch (IOException e) {System.out.println ("Erorr");
System.exit (0); } public void Run () {try{while (mstrmsg = In.readline ())!=null) {if (Mstrmsg.trim (). Equa
LS ("exit")) {Clients.remove (msocket);
In.close ();
Out.close (); mstrmsg = "User:" + msocket.getinetaddress () + "Exit Tatal:" + clients.size();
;
Msocket.close ();
Sendmassage ();
Break
} else{mstrmsg = msocket.getinetaddress () + ":" + mstrmsg;
Sendmassage ();
}}catch (IOException e) {System.out.println ("Erorr");
System.exit (0);
}} public static void Main (string[] args) {new TCPServer ();
}
}
2.tcpclient.java
Import java.net.*;
Import java.io.*;
Import java.util.concurrent.*;
public class TcpClient {private static final int PORT = 8888;
Private Socket Client = null;
private BufferedReader sin = null;
Private Executorservice Mexecutorservice;
Public TcpClient () {try{Client = new Socket ("120.27.126.174", PORT);
Sin = new BufferedReader (New InputStreamReader (Client.getinputstream (), "GB2312"));
Mexecutorservice = Executors.newcachedthreadpool ();
Mexecutorservice.execute (New Threadclient (Client));
String msg = NULL;
while (msg = Sin.readline ())!= null) {System.out.println (msg);
}}catch (IOException e) {System.out.println (E.getmessage ());
} class Threadclient extends thread{private Socket msocket = null;
Private String msg = null;
BufferedReader in = null;
PrintWriter out = null;
Public threadclient (socket socket) {this.msocket = socket;
public void Run () { try{in = new BufferedReader (new InputStreamReader (system.in));
out = new PrintWriter (New OutputStreamWriter (Msocket.getoutputstream (), "GB2312"), true);
while (true) {msg = In.readline ();
OUT.PRINTLN (msg);
if (Msg.trim (). Equals ("Exit")) {in.close ();
Out.close ();
Mexecutorservice.shutdownnow ();
Break
}}catch (IOException e) {System.out.println ("you");
System.exit (0);
}} public static void Main (string[] args) {new TcpClient ();
}
}
The above is the Java implementation of Tcpsocket chat room function code, I hope to help you learn.