tcp程序设计--客户端获取服务器输入输出流

tcp程序设计--客户端获取服务器输入输出流

思路:

第一步:实例化一个ServerSocket对象(服务器套接字),用来等待网络上的请求(也就是等待来连接的套接字)

第二步:调用accept()方法,返回一个与客户端socket对象相连接的socket对象

第三步:服务器端socket对象使用getOutputStream方法获得的输出流将指向客户端socket对象使用getInputStream获得的输入流,反之亦然

服务器端代码:

 1 public class Server {
 2     public static void main(String[] args) {
 3         try {
 4             ServerSocket ss = new ServerSocket(8001);
 5             while (!ss.isClosed()) {
 6                 Socket s = ss.accept();
 7                 OutputStream ops = s.getOutputStream();
 8                 String str = "欢迎进入程序\n编写TCP服务器程序,"
 9                         + "实现创建一个在8001端口上等待的ServerSocket"
10                         + "对象,当接收到一个客户机的连接请求后,"
11                         + "程序从与客户机建立了连接的Socket对象中获得输入输出"
12                         + "流。通过输出流向客户机发送信息。";
13                 ops.write(str.getBytes());  //将字符串变成byte数组写入
14                 ops.close();
15                 s.close();
16             }
17             ss.close();
18         } catch (IOException e) {
19             e.printStackTrace();
20         }
21     }
22 }

客户端代码:

 1 public class Client extends JFrame {
 2     
 3     private static final long serialVersionUID = 1L;
 4     private JTextArea textArea;
 5     private JTextField portField;
 6     private JTextField hostField;
 7     
 8     public static void main(String args[]) {
 9         
10             Client frame = new Client();
11             frame.setVisible(true);
12             }
13     
14     public Client() {
15         super();
16         setBounds(100, 100, 500, 212);
17         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18         
19         final JPanel panel = new JPanel();
20         panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
21         getContentPane().add(panel, BorderLayout.NORTH);
22         
23         final JLabel label = new JLabel();
24         label.setText("连接主机:");
25         panel.add(label);
26         
27         hostField = new JTextField();
28         hostField.setText("localhost");
29         panel.add(hostField);
30         
31         final JLabel label_1 = new JLabel();
32         label_1.setText("端口:");
33         panel.add(label_1);
34         
35         portField = new JTextField();
36         portField.setText("8001");
37         panel.add(portField);
38         
39         final JButton button = new JButton();
40         button.addActionListener(new ActionListener() {
41             public void actionPerformed(final ActionEvent e) {
42                 final String hostName = hostField.getText();
43                 String portNum = portField.getText();
44                 final int port = Integer.parseInt(portNum);
45                         try {
46                             final InetAddress host = InetAddress.getByName(hostName);//实例化对应主机的InetAddress对象
47                             Socket socket = new Socket(host, port); //实例化socket
48                             final InputStream is = socket.getInputStream();  //对应服务器端的getoutputstream
49                             InputStreamReader reader=new InputStreamReader(is);
50                             int data = 0;
51                             while ((data=reader.read()) != -1) {
52                                 textArea.append((char)data+"");
53                             }
54                         } catch (Exception e1) {
55                             textArea.append(e1.toString());
56                             e1.printStackTrace();
57                         }
58                     }
59         });
60         button.setText("连接");
61         panel.add(button);
62         
63         final JScrollPane scrollPane = new JScrollPane();
64         getContentPane().add(scrollPane, BorderLayout.CENTER);
65         
66         textArea = new JTextArea();
67         textArea.setLineWrap(true);  //自动换行
68         scrollPane.setViewportView(textArea);
69     }
70 }

结果如下:

备注:

BoxLayout 箱式布局
BoxLayout 可以把控件依次进行水平或者垂直排列布局,这是通过参数 X_AXIS、Y_AXIS 来决定的。X_AXIS 表示水平排列,而 Y_AXIS 表示垂直排列。BoxLayout 的构造函数有两个参数,一个参数定义使用该 BoxLayout 的容器,另一个参数是指定 BoxLayout 是采用水平还是垂直排列

posted @ 2018-03-24 22:18  xtu熊大  阅读(452)  评论(0编辑  收藏  举报