Java Network programming notes 6, java Network programming notes

Source: Internet
Author: User

Java Network programming notes 6, java Network programming notes

In the previous articles, we have known the communication between the client and the server, including the communication between multiple clients and the server. Next we will demonstrate the complete communication process between the client and the server.


First look

Client 1

 

Client 2



Server


Server Thread class:

<Span style = "font-size: 18px;"> public class Connection extends Thread {private JTextArea txt; private Socket st; private String msg = null; private BufferedReader 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 (); ps = new PrintStream (new DataOutputStream (st. getOutputStream (); while (true) {msg = br. readLine (); txt. append ("received information from the client" + msg + '\ n'); Server. send (msg) ;}} catch (Exception e) {e. printStackTrace () ;}} public void send (String msg) {ps. println (msg) ;}}</span>

Connect the server to the client:

<Span style = "font-size: 18px;"> public class Server extends JFrame {private JTextArea txt; private ServerSocket ss; private static List <Connection> conns = new ArrayList <Connection> (); public 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 (3000); 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 master 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 JPanel pl; private JButton bt; private BufferedReader br; private DataOutputStream out; private PrintStream ps; private Container f = this. getContentPane (); public Client () {f. setLayout (new BorderLayout (); txta = new JTextArea (); f. add (txta, BorderLayout. CENTER); txtf = new JTextField (20); bt = new JButton ("send"); pl = new JPanel (); pl. setLayout (new FlowLayout (); pl. add (txtf); pl. add (bt); bt. addActionListener (this); f. add (pl, BorderLayout. SOUTH); setTitle ("client"); setSize (500,300); setVisible (true); run (); new Thread () {start ();} public void run () {while (true) {try {txta. append ("received message:" + br. readLine () + "\ n");} catch (Exception e) {e. printStackTrace () ;}}};} public void run () {try {Socket socket = new Socket ("192.168.12.112", 3000); out = new DataOutputStream (socket. getOutputStream (); ps = new PrintStream (out); br = new BufferedReader (new InputStreamReader (socket. getInputStream ();} catch (Exception e) {e. printStackTrace () ;}@ Overridepublic void actionreceivmed (ActionEvent e) {if (e. getSource () = bt) {String msg = txtf. getText (); try {ps. println (msg); txta. append ("sent message:" + 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>




Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/42804309 sentiment control _


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.