[Java] I/O underlying principle two: socket working mechanism

Source: Internet
Author: User

First, TCP state conversion

The status transition diagram for the TCP connection is as follows

Note: SYN indicates a link is established, FIN indicates a close link, ACK indicates a response, PSH represents a data transfer, and RST represents a link reset.

    • CLOSED: The initial state, which also enters this state when the time-out or connection is closed.
    • LISTEN: The state of the server when it waits for a connection.
    • Syn-sent: The state that the client initiates a connection and sends a SYN to the server after it waits. If you cannot connect, enter the CLOSED state.
    • SYN-RCVD: The server accepts a SYN request from the client, which is entered in this state by the LISTEN state. Respond to a SYN, ACK to the client at the same time.
    • Established: The state of the data transfer, the status of the server and the client after the connection is established.
    • Fin-wait-1: The active closed party enters this state. Waiting for a remote TCP connection to interrupt the request, or the acknowledgement of a previous connection interrupt request
    • Fin-wait-2: The active closed party receives the other party's FIN ack into the state.
    • Close-wait: The passive closed party receives the fin after it enters the state and receives the fin while sending an ACK.
    • Last-ack: A closed request is initiated by a passive closed party.
    • CLOSING:
    • Time-wait:

1 steps to establish a connection (three-time handshake)

    1. The client sends the SYN to the server
    2. Service-side response ACK and SYN to client
    3. The client sends an ACK to the server

2 Steps to break a link (four waves)

    1. The client sends FIN to the service side
    2. Service-side Response ACK
    3. server close link, send FIN to Client
    4. The client responds with an ACK to the server

Second, Socket

A socket instance represents a communication link, and when the connection is established successfully, both the server and the client each have a socket instance, and the two instances will have a inputstream and outputstream to transfer the data. There will be a buffer in InputStream and outputstream, and data writes and reads are done through this buffer. The data is written to its SENDQ queue through OutputStream, and when the queue fills up, the data is passed to the RECVQ queue of InputStream, and if RECVQ is full then OutputStream's write () method will block until The RECVQ queue has enough space to hold the data sent by SENDQ. It is important to note that the size of the buffer and the reading and writing quarters greatly affect the transmission efficiency of the connection, and because it is a blocking type, if two times the simultaneous transmission of data may occur deadlock.

Socket Source Analysis:

Public socket ()//create a non-connected socket

Public Socket (proxy)//

[Java] I/O underlying principle two: socket working mechanism

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.