网络Socket编程(简易qq实现之C/S通信1)

1. 目标:实现两个用户之间的通信,利用的是简单的Socket知识以及简略界面

2. 界面:分为客户端与服务器端(如下图)

网络Socket编程(简易qq实现之C/S通信1)_第1张图片

网络Socket编程(简易qq实现之C/S通信1)_第2张图片

3. 基本功能:客户端先向服务器端发送一个消息,这样就可以让客户端与服务器端实现持续通信

4. 代码实现:

(1)服务器端代码

 1 package com.test;
 2 
 3 import java.awt.event.ActionEvent;
 4 import java.awt.event.ActionListener;
 5 import java.io.BufferedReader;
 6 import java.io.IOException;
 7 import java.io.InputStreamReader;
 8 import java.io.PrintWriter;
 9 import java.net.ServerSocket;
10 import java.net.Socket;
11 
12 import javax.swing.JButton;
13 import javax.swing.JFrame;
14 import javax.swing.JPanel;
15 import javax.swing.JScrollPane;
16 import javax.swing.JTextArea;
17 import javax.swing.JTextField;
18 
19 public class QQServer extends JFrame implements ActionListener{
20     JTextArea jta;
21     JTextField jtf;
22     JButton jb;
23     JScrollPane jsp;
24     JPanel jpn;
25     
26     PrintWriter pw;
27     /**
28      * @param args
29      */
30     public static void main(String[] args) {
31         QQServer qq = new QQServer();
32         qq.init();
33     }
34     public QQServer(){
35         //页面布局
36         jta = new JTextArea();
37         jtf = new JTextField(10);
38         jb  = new JButton("发送");
39         jb.addActionListener(this);//事件监听
40         jsp = new JScrollPane(jta);
41         jpn = new JPanel();
42         
43         jpn.add(jtf);
44         jpn.add(jb);
45         
46         this.add(jsp,"Center");
47         this.add(jpn,"South");
48         this.setTitle("简易qq服务器端");
49         
50         this.setSize(300,200);
51         this.setVisible(true);
52     }
53     public void init(){
54         try {
55             //服务器监听端口
56             System.out.println("服务器在监听.....");
57             ServerSocket ss = new ServerSocket(9992);
58             Socket s = ss.accept();
59             //服务器接收流
60             InputStreamReader isr = new InputStreamReader(s.getInputStream());
61             BufferedReader br = new BufferedReader(isr);
62             //服务器输出
63             pw = new PrintWriter(s.getOutputStream(), true);
64             while (true) {
65                 //持续接收从客户端信息并显示
66                 String info = br.readLine();
67                 jta.append("客户端对服务器端说:"+info+"\r\n");
68             }
69         } catch (Exception e) {
70             // TODO: handle exception
71         }
72     }
73     @Override
74     public void actionPerformed(ActionEvent e) {
75         //在Jtextfield内获取文字,并发送给客户端
76         if(e.getSource()==jb){
77             String msg = jtf.getText();
78             jta.append("服务器对客户端说:"+msg+"\r\n");
79             pw.println(msg);
80             jtf.setText("");
81         }
82         
83     }
84 
85 }
View Code

(2)客户端代码

 1 package com.test;
 2 /**
 3  * 简易qq,服务器端实现
 4  */
 5 import java.awt.event.ActionEvent;
 6 import java.awt.event.ActionListener;
 7 import java.io.BufferedReader;
 8 import java.io.InputStreamReader;
 9 import java.io.PrintWriter;
10 import java.net.Socket;
11 
12 import javax.swing.JButton;
13 import javax.swing.JFrame;
14 import javax.swing.JPanel;
15 import javax.swing.JScrollPane;
16 import javax.swing.JTextArea;
17 import javax.swing.JTextField;
18 
19 public class QQClient extends JFrame implements ActionListener{
20     JTextArea jta;//
21     JTextField jtf;
22     JButton jb;
23     JScrollPane jsp;
24     JPanel jpn;
25     
26     PrintWriter pw;
27     public static void main(String[] args) {
28         QQClient qq = new QQClient();
29         qq.init();
30     }
31     public QQClient(){
32         //界面布局
33         jta = new JTextArea();
34         jtf = new JTextField(10);
35         jb  = new JButton("发送");
36         jb.addActionListener(this);
37         jsp = new JScrollPane(jta);
38         jpn = new JPanel();
39         
40         jpn.add(jtf);
41         jpn.add(jb);
42         
43         this.add(jsp,"Center");
44         this.add(jpn,"South");
45         this.setTitle("简易qq 客户端");
46         
47         this.setSize(300,200);
48         this.setVisible(true);
49         
50     }
51     public void init(){
52         try {
53             //客户端请求服务器
54             Socket s = new Socket("127.0.0.1",9992);
55             //客户端接收流
56             InputStreamReader isr = new InputStreamReader(s.getInputStream());
57             BufferedReader br = new BufferedReader(isr);
58             //客户端输出
59             pw = new PrintWriter(s.getOutputStream(), true);
60             while (true) {
61                 //持续接收从服务器信息并显示
62                 String info = br.readLine();
63                 jta.append("服务器端对客户端说:"+info+"\r\n");
64             }
65         } catch (Exception e) {
66             // TODO: handle exception
67         }
68     }
69     @Override
70     public void actionPerformed(ActionEvent e) {
71         //在Jtextfield内获取文字,并发送给服务器端
72         if(e.getSource()==jb){
73             String msg = jtf.getText();
74             jta.append("客户端对服务器端说:"+msg+"\r\n");
75             pw.println(msg);
76             jtf.setText("");
77         }
78         
79     }
80 
81 }
View Code

 

转载于:https://www.cnblogs.com/changna1314/p/6818182.html

你可能感兴趣的:(网络Socket编程(简易qq实现之C/S通信1))