TCP transmission instance of Java implementation socket

Source: Internet
Author: User
Tags resource socket socket connect

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.