This article illustrates the TCP transmission of Java implementation socket. Share to everyone for your reference. The specific analysis is as follows:
Client sends data to server
* TCP transmission, the process of client establishment.
* 1, create the TCP client socket service. The socket object is used.
* It is recommended that the object be clearly defined as soon as it is created. The host to connect to.
* 2, if the connection is established successfully, the data transfer channel has been established.
* This channel is the socket stream, is the bottom set up. Since it is a stream, it shows that there is both input and output.
* Want to input or output stream objects, you can find a socket to get.
* Getoutputstream (), and getInputStream () can be used to get two bytes of stream.
* 3, using the output stream, write the data.
* 4, close the resource.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
The server receives the data sent over by the client and prints it on the console.
* Establish the TCP service side of the idea:
* 1, create the service-side socket service. Through the ServerSocket object.
* 2, the service side must provide a port, otherwise the client can not connect.
* 3, get the client object that is connected.
* 4, obtaining the socket stream from the client object to read the data sent by the client
* and print on the console.
* 5, close the resource. Close the client, turn off the service side.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The |
|
Run Effect chart: (Start the server first, then start the client)
I hope this article will help you with your Java programming.