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)
- The client sends the SYN to the server
- Service-side response ACK and SYN to client
- The client sends an ACK to the server
2 Steps to break a link (four waves)
- The client sends FIN to the service side
- Service-side Response ACK
- server close link, send FIN to Client
- 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