《安卓网络编程》之第二篇 java环境下网络通信的综合应用

  经过前面内容的学习,我们了解了Java技术中实现网络通信的基本知识。下面将通过一个具体视力的实现过程,讲解客户端和服务器端通信的流程。

服务器端的实现文件是 Server.java,代码如下:

 1 import java.io.*;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 import javax.swing.*;
 5 import java.net.*;
 6 import java.util.*;
 7 
 8 class Conn extends Thread{
 9     private JTextArea txt;
10     private Socket st;
11     private String msg = null;
12     private BufferedReader br = null;
13     private PrintStream ps;
14     public Conn(Socket st,JTextArea txt){
15         this.st = st;
16         this.txt = txt;
17         start();
18     }
19     public void run(){
20         try{
21             br = new BufferedReader(new InputStreamReader(st.getInputStream()));
22             ps = new PrintStream(new DataOutputStream(st.getOutputStream()));
23         }catch(Exception e){
24             System.err.println("input failed");
25         }
26         while(true){
27             try{
28                 msg = br.readLine();
29                 txt.append("从客户端收到信息:"+msg+'\n');
30                 txt.append("信息接受时间是:"+new Date()+"\n");
31                 Server.send(msg);
32             }catch(Exception e){
33                 System.err.println("connection closed");
34                 break;
35             }
36         }
37     }
38     public void send(String msg){
39       ps.println(msg);
40     }
41 }
42 public class Server extends JFrame{
43     private JTextArea txt;
44     private ServerSocket ss;
45     private static java.util.List<Conn> conns = new ArrayList<Conn>();
46     public Server(){
47        txt = new JTextArea();
48        this.setTitle("服务器");
49        this.setLayout(new BorderLayout());
50        this.add(new JScrollPane(txt),BorderLayout.CENTER);
51        this.setSize(500,300);
52        this.setVisible(true);
53        run();
54     }
55     public void run(){
56         try{
57             ss = new ServerSocket(8000);
58         }catch(Exception e){
59             System.err.println("open socket failed");
60         }
61         txt.append("服务器已经启动!"+"\n");
62         while(true){
63             try{
64                 Socket st=ss.accept();
65                 conns.add(new Conn(st,txt));
66             }
67             catch(IOException ex){
68                 System.err.println(ex);
69             }
70         }
71     }
72     public static void send(String msg){
73       for(Conn c:conns)
74         c.send(msg);
75     }
76     public static void main(String args[]){
77         Server myserver=new Server();
78     }
79 }

客户端的实现文件是Client.java,代码如下:

 1 import java.io.*;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 
 5 import javax.swing.*;
 6 
 7 import java.net.*;
 8 import java.util.*;
 9 
10 public class Client extends JFrame implements ActionListener{
11     final JTextArea txta;
12     JTextField txtf;
13     JPanel pl;
14     JButton bt;
15     BufferedReader br;
16     DataOutputStream out;
17     PrintStream ps;
18     Container f = this.getContentPane();
19         public Client() {
20             f.setLayout(new BorderLayout());
21             txta = new JTextArea();
22             f.add(txta,BorderLayout.CENTER);
23             txtf = new JTextField(20);
24             bt = new JButton ("发送");
25             pl = new JPanel();
26             pl.setLayout(new FlowLayout());
27             pl.add(txtf);
28             pl.add(bt);
29             bt.addActionListener(this);
30             f.add(pl,BorderLayout.SOUTH);//The south layout constraint (bottom of container).
31             setTitle("信息发送端");
32             setSize(500,300);
33             setVisible(true);
34             run();
35             Thread t = new Thread(new Runnable() {
36                 
37                 public void run() {
38                     while(true) {
39                         
40                         try{
41                             txta.append("收到消息:"+br.readLine()+"\n");
42                         }catch(Exception ex) {}
43                     }
44                     
45                 }
46             });
47             t.start();
48         }
49 
50     public void actionPerformed(ActionEvent e) {
51         // TODO Auto-generated method stub
52         if(e.getSource() == bt) {
53             String msg = txtf.getText();
54             try{
55                 ps.println(msg);
56                 txta.append("已经发送消息:"+msg+"\n");
57             }catch(Exception ex){
58                 txta.append(ex.toString()+"\n");
59             }
60         }
61     }
62     private void run() {
63         try
64         {
65             Socket sc= new Socket("127.0.0.1",8000);
66             out = new DataOutputStream(sc.getOutputStream());
67             ps = new PrintStream(out);
68             br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
69         }
70         catch(IOException ex)
71         {
72             txta.append(ex.toString()+"\n");
73         }
74         
75         
76     }
77 
78 
79     public static void main(String[] args) {
80         Client myclient = new Client();
81     }
82 
83     
84 }

同样是先执行服务器端,在执行客户端,效果如下:

 

posted @ 2015-03-24 22:56  疾风剑  阅读(219)  评论(0编辑  收藏  举报