Socket network programming example of Java network programming (server / client)
Java provides two classes for TCP protocol, which are used in client-side programming and server-side programming respectively. Before the application starts communication, a connection needs to be created and initiated by the client program; The server-side program needs to always listen to the specific port number of the host and wait for the client to connect. In the client, we only need to use the socket instance, while the server should handle both the ServerSocket instance and the socket instance; Both also use OutputStream and InputStream to send and receive data.
The best way to learn a knowledge is to use it. Through the previous notes, we already know how to obtain the address information of the host. Now we preliminarily learn the socket programming of the transport layer using TCP protocol through a simple program.
TCP server side
In socket programming, the server side is much more complex than the client side. The work of the server is to establish a communication terminal and passively wait for the connection of the client. The following server-side program example is used to listen to the port number obtained from the console input, and send back the message sent by the client.