Socket Series Client Socket--socket class

Source: Internet
Author: User
Tags java throws

Assuming that the TCP socket server is already established and listening to the client's connection, the client can initiate the connection through the socket class. After the client initiates a connection request, it passively waits for a response from the server. This class is also located in the java.net package, which contains many methods for establishing connections, manipulating data flows, and so on. The client works by following several steps:
① Create a socket instance, and the constructor directly specifies the remote server IP and port to establish a TCP connection.
Ii Through the input and output stream of the socket instance, the socket instance contains a InputStream object and a OutputStream object, which can be used to send data by manipulating these streams.
③ After the communication is complete, close the connection with the close () method of the socket instance.
It is understood how the application layer Java works, and then it is necessary to study the socket from the application layer to the bottom of the system how to work, the application layer of these operations at the bottom of the system how to react. As shown in 2-3-2-3, the dotted line is the boundary, upper-level application layer, the lower layer is the system bottom. The overall workflow can be divided into the following steps:
(1) First determine the target to communicate with, including the destination IP and destination port.
(2) Create a socket instance at the Java application layer based on the destination IP and port.
(3) Block wait and prepare for the underlying work on the system.
(4) Create socket underlying data structure, socket initial state is off.
(5) Fill the socket with the local, remote address and port, and send a connection request to the remote server, at which point the socket status is being connected.
(6) After completing the 3 handshake with the remote server, the connection is established and the socket status is completed.
(7) Complete the socket instantiation on the application layer, and the socket can then be manipulated to achieve communication.

Figure 2-3-2-3 Socket Bottom operating principle


In actual communication, the client socket does not clearly specify which local port number to use, but in fact the system randomly assigns a port number (typically greater than 1023), so the local port number used for each communication is generally different. In addition, because messages may be delayed in the network, and if the server client socket connection is turned off and the server with the client socket is generated with the same address, the old messages that are delayed in the network are mistaken for a new connection message to be sent to the new socket connection, causing an error. So the TCP specification requires at least one socket to remain in the time-wait state for a period of time after the handshake is closed on both ends. The socket of the general client becomes time-wait after the communication is complete and remains for a period of time. During this time, the socket is not allowed to use this local port number, and the application layer Java throws an IOException exception if it attempts to create a new socket instance with that port number.

Socket Series Client Socket--socket class

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.