tcp programming -- client obtains server input and output stream

Keywords: Java socket Programming network

tcp programming -- client obtains server input and output stream

Train of thought:

Step 1: instantiate a ServerSocket object (server socket) to wait for requests on the network (that is, the socket waiting to connect)

Step 2: call the accept() method to return a socket object connected to the client socket object

Step 3: the output stream obtained by the server socket object using getOutputStream method will point to the input stream obtained by the client socket object using getInputStream, and vice versa

Server code:

 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 = "Welcome to the program\n To write TCP Server program,"
 9                         + "Implementation to create a waiting on port 8001 ServerSocket"
10                         + "Object, when a client's connection request is received,"
11                         + "The program establishes a connection from the Socket Object to get input and output"
12                         + "Flow. Send information to the client through the output.";
13                 ops.write(str.getBytes());  //Change string to byte Array write
14                 ops.close();
15                 s.close();
16             }
17             ss.close();
18         } catch (IOException e) {
19             e.printStackTrace();
20         }
21     }
22 }

Client code:

 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("Connect host:");
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("Port:");
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);//Instantiate the InetAddress object
47                             Socket socket = new Socket(host, port); //instantiation socket
48                             final InputStream is = socket.getInputStream();  //Corresponding to the server-side 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("Connect");
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);  //Automatic line feed
68         scrollPane.setViewportView(textArea);
69     }
70 }

The results are as follows:

Remarks:

BoxLayout box layout
BoxLayout can arrange the controls horizontally or vertically in turn, which is determined by the parameters x ﹣ axis and Y ﹣ axis. X ﹣ axis stands for horizontal arrangement, while y ﹣ axis stands for vertical arrangement. The constructor of BoxLayout has two parameters. One parameter defines the container using the BoxLayout, and the other parameter specifies whether the BoxLayout is arranged horizontally or vertically

Posted by bgoulette612 on Sat, 04 Apr 2020 12:56:32 -0700