In the previous articles, we have learned that the client communicates with the server, including multiple client-to-server communications, and then we demonstrate the complete communication process between the client and the server.
Look first.
Client 1
Client 2
Server-side
Server Thread class:
<span style= "FONT-SIZE:18PX;" >public class Connection extends Thread {private JTextArea txt;private Socket st;private String msg = null;private Buff Eredreader br = null;private printstream ps;public Connection (Socket St, JTextArea txt) {this.st = St;this.txt = Txt;start ();} @Overridepublic void Run () {try {br = new BufferedReader (new InputStreamReader (New DataInputStream (St.getinputstream ()) ));p s=new PrintStream (New DataOutputStream (St.getoutputstream ())), while (true) {msg=br.readline (); Txt.append (" Received the message from the client "+msg+ ' \ n '); Server.send (msg);}} catch (Exception e) {e.printstacktrace ();}} public void Send (String msg) {ps.println (msg);}} </span>
The server connects to the client:
<span style= "FONT-SIZE:18PX;" >public class Server extends JFrame {private JTextArea txt;private serversocket ss;private static LIST<CONNECTION&G T Conns=new arraylist<connection> ();p ublic server () {txt=new jtextarea (); This.settitle ("server"); This.setlayout ( New BorderLayout ()); This.add (new JScrollPane (TXT), borderlayout.center); This.setsize (500,300); This.setvisible ( true); run ();} private void Run () {try {ss=new serversocket, txt.append ("Accept time:" +new Date () + "\ n"), while (true) {Socket st= Ss.accept (); Conns.add (New Connection (St, TXT));}} catch (Exception e) {e.printstacktrace ();}} public static void Send (String msg) {for (Connection C:conns) {c.send (msg);}}} </span>
Server Main Portal:
<span style= "FONT-SIZE:18PX;" >public class Servermain {public static void main (string[] args) {server server=new server ();}} </span>
Client:
<span style= "FONT-SIZE:18PX;" >public class Client extends JFrame implements ActionListener {private JTextArea txta;private JTextField txtf;private J Panel pl;private JButton bt;private bufferedreader br;private dataoutputstream out;private printstream ps;private Container f = this.getcontentpane ();p ublic Client () {f.setlayout (New BorderLayout ()); txta = new JTextArea (); F.add (Txta, TXTF = new JTextField (borderlayout.center), BT = new JButton ("Send");p L = new JPanel ();p l.setlayout (New FlowLayout ()); Pl.add (TXTF);p L.add (BT) Bt.addactionlistener (this); F.add (PL, Borderlayout.south); Settitle ("client"); SetSize (500, SetVisible (True); run (); new Thread () {{start ();} public void Run () {while (true) {try {txta.append ("received message:" + br.readline () + "\ n")} catch (Exception e) {E.printstacktrac E ();}}}};} public void Run () {try {socket socket=new socket ("192.168.12.112"), Out=new DataOutputStream ( Socket.getoutputstream ());p s=new PrintStream (out); Br=new BufferedReader (New InputStreamReader (SockeT.getinputstream ()));} catch (Exception e) {e.printstacktrace ();}} @Overridepublic void actionperformed (ActionEvent e) {if (E.getsource () ==bt) {String msg=txtf.gettext (); try {ps.println (msg); Txta.append ("Message already Sent:" +msg+ "\ n");} catch (Exception E2) {e2.printstacktrace ();}}}} </span>
Client Portal:
<span style= "FONT-SIZE:18PX;" >public class Clientmain {public static void main (string[] args) {Client client=new client ()}} </span>
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/42804309 Emotional Control _
Java Network Programming note 6