Java share notes: Java Network Programming -- TCP programming

[1] Main steps of TCP programming

Client:

1. To create a Socket object, InetAddress class object and int type value are required in the parameter list of the constructor to indicate the IP address and port number of the other party.

2. An object of the subclass of the OutputStream abstract class is returned through the getOutputStream() method of the Socket object to send the output stream.

3. Output specific information through the write method of the output stream.

4. Close the corresponding flow and Socket objects.

Server:

1. create the object of ServerSocket class and indicate the port number in the constructor.

2. Call the accept() method of the ServerSocket class object to return an instance of the Socket class.

3. Get an input stream through getInputStream() method of Socket instance to receive information from the client.

4. Use input stream to receive data and process data.

5. Close the corresponding stream, Socket object and ServerSocket object.

 

[2] Java source program (Note: when testing, first open the server method, and then the client method, client())

 1 package pack01;
 2 
 3 import java.io.*;
 4 import java.net.*;
 5 import org.junit.Test;
 6 
 7 public class TestNet1 {
 8     
 9     @Test //***********************Client test method***********************
10     public void client() {
11         
12         Socket socket = null;    //Set up client network socket
13         OutputStream socket_os = null;  //Client output stream
14         
15         try {
16             //1.Get local loop address
17             InetAddress inet = InetAddress.getByName("127.0.0.1");
18             
19             //2.Establish Socket object
20             socket = new Socket(inet, 10000);
21             
22             //3.Get output stream
23             socket_os = socket.getOutputStream();
24             
25             //4.Client output information
26             socket_os.write( "Client sends information".getBytes() );
27             
28         } catch (IOException e) {
29             e.printStackTrace();
30             
31         } finally {
32             
33             try { //Close output stream
34                 socket_os.close();
35             } catch (IOException e) {
36                 e.printStackTrace();
37             }
38             
39             try { //Close Client Socket 
40                 socket.close();
41             } catch (IOException e) {
42                 e.printStackTrace();
43             }
44         }
45     }
46     
47     @Test  //***********************Server test method***********************
48     public void server() {
49         
50         ServerSocket serSocket = null;
51         Socket socket = null;
52         InputStream socket_is = null;
53         
54         try {
55             serSocket = new ServerSocket(10000);
56             socket = serSocket.accept(); //Get server socket
57             socket_is = socket.getInputStream(); //Get input stream
58             
59             byte[] b = new byte[100]; //Byte array for receiving information
60             int len;
61             StringBuffer sb = new StringBuffer();
62             
63             while( (len = socket_is.read(b)) != -1 ) {
64                 sb.append( new String(b,0,len) ); //Save byte information continuously in buffer Array
65             }
66             System.out.println("Come from" + socket.getInetAddress().getHostName() + "Information:");
67             System.out.println( sb );
68             
69         } catch (IOException e) {
70             e.printStackTrace();
71             
72         } finally {
73             
74             try { //Close input stream
75                 socket_is.close();
76             } catch (IOException e) {
77                 e.printStackTrace();
78             }
79             
80             try { //Close Socket object
81                 socket.close();
82             } catch (IOException e) {
83                 e.printStackTrace();
84             }
85             
86             try { //Close ServerSocket object
87                 serSocket.close();
88             } catch (IOException e) {
89                 e.printStackTrace();
90             }
91         }
92     }
93 
94 }

 

Note: I hope to communicate with you and make progress together.

Tags: Java socket Programming Junit

Posted on Sun, 05 Jan 2020 23:08:03 -0800 by nonexistence