TCP transmission instance of socket implemented in Java

This article describes the implementation of socket TCP transmission in Java. Share with you for your reference. The specific analysis is as follows:

The client sends data to the server

*TCP transmission, the process of client establishment* 1. Create a TCP client socket service. Socket object is used* It is recommended that the destination be specified as soon as the object is created. The host to connect to* 2. If the connection is established successfully, the data transmission channel has been established* This channel is the socket stream, which is established at the bottom. Since it is a stream, it means that there are both inputs and outputs* If you want to input or output stream objects, you can find socket to obtain them* You can get two byte streams through getoutputstream() and getinputstream()* 3. Write out the data using the output stream* 4. Close the resource.

The server receives the data sent by the client and prints it on the console.

*Ideas for establishing TCP server: * 1. Create a socket service on the server. Through the ServerSocket object* 2. The server must provide an external port, otherwise the client cannot connect* 3. Get the connected client object* 4. Obtain the socket stream through the client object, read the data * sent by the client and print it on the console* 5. Close the resource. Close the client and close the server.

Operation effect diagram: (start the server first, and then the client)

I hope this article will be helpful to your Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>