This paper is participating in theNetwork protocols must be known and must be known”Essay campaign
Net package – based small application
The complete code has been uploaded to Github github-TCP
Welcome star and Issue
TCP is introduced
The characteristics of
-
Connection-oriented transport layer protocol. TCP connections must be established before applications can use TCP. After data transfer is complete, the established TCP connection must be released.
-
Each TCP connection can have only two endpoints, and each TCP connection can be point-to-point only.
-
TCP provides reliably delivered services.
Data transmitted over a TCP connection is error-free, not lost, not duplicated, and arrives sequentially.
- TCP provides full duplex communication.
TCP allows applications on both sides of the communication to send data at any time.
- Byte – oriented stream.
A TCP stream is a sequence of bytes flowing into or out of a process. Although an application interacts with TCP one data block at a time, TCP treats the data handed over by the application as just a series of unstructured byte streams.
The illustration
- TCP structure
- A TCP connection
TCP connection established, three-way handshake
Transfer control block TCB: Stores some important information in each connection. Such as TCP connection tables, Pointers to send and receive buffers, Pointers to retransmission queues, current send and receive sequences, and so on.
Suppose host A is the TCP client and host B is the TCP server program. At first, the TCP processes at both ends are CLOSED. The connection is opened on client A, and the connection is opened on the server. At the beginning, THE TCP server process of B creates the transfer control block TCB and prepares to accept the connection request of the client process. Then, the server process is in the LISTEN state, waiting for the connection request of A.
-
Then A’s process first creates the transport control module TCB. Sends a connection request segment to B with SYN=1 in the header and an initial sequence number, seq=x. According to TCP, the SYN packet segment (SYN=1) cannot write data, but consumes a sequence number. At this point, A enters the synchronous sent state.
-
After receiving the connection request packet, user B agrees to establish A connection, and sends an acknowledgement to USER A. In the acknowledgement packet, user B sets the SYN bit and AVK position to 1, the acknowledgement number is ACK +1, and selects an initial serial number Y. Similarly, this packet segment cannot write data, but consumes a sequence number. In this case, USER B enters the synchronous receive state.
-
After A receives B’s confirmation, it also needs to give B its confirmation. The ACK of the packet segment is set to 1, the ACK number is Y +1, and the seq of the packet segment is X +1. The ACK packet segment can carry data. However, if it does not carry data, the sequence number is not consumed. In this case, the sequence number of the next data packet segment is seq= X +1.
At this point TCP is already established. A enters the phase in which the connection has been established. User B also enters the connection state after receiving the confirmation.
TCP connection released, four waves
After the data transfer is complete, both sides of the communication can release the connection. Now both A and B are in ESTABLISHED state.
- The application process of A sends A connection release packet segment to the TCP, stops sending data, and closes the TCP connection. A releases the terminate control bit at the header of the link segment
FIN
Set to1
And its serial number isseq=u
, which is equal to the ordinal number of the last byte of data transmitted before plus1
At this moment A entersFin-wait-1 (terminate WAIT 1)
Status, waiting for B’s confirmation.
Note: TCP specifies that a FIN segment consumes a sequence number even if it does not carry data.
- B sends an acknowledgement after receiving the link release packet. The acknowledgement number is
ack = u + 1
, and the sequence number of the packet segment isv
Is equal to the data B has transmitted beforeThe ordinal number of the last byte is incremented by 1
.Then B entersClose-wait (CLOSE WAIT)
State. The TCP server process notifies the higher-level application process that the connection from A to B is released. The TCP connection is half-closed, that is, A has no data to send, but if B sends data, A still receives it, that is, the connection from B to A is not closed. It’s likely to stay that way for a while. - A enters after receiving confirmation from B
Fin-wait-2 (terminate WAIT 2)
The link release packet sent by B is in full state. If B has no data to send to A, the application process notifies TCP to release the connection. In this case, the connection release packet segment sent by B must be enabledFIN = 1
, now assume that the serial number of B isw
(B may send some more data in the half-closed state). B must also repeat the confirmation number that was sent last timeack = u + 1
.Then B entersLast-ack (final confirmation)
Status, waiting for A’s confirmation. - After receiving the link release segment from B, A must send an acknowledgement. In the acknowledgement message segment
ACK to one
And confirm,ack=w+1
, and its serial number isseq=u+1
(According to the TCP standard, the FIN segment previously sent consumes a sequence number.) And then go intoTIME-WAIT
(Time wait) status.Note:Now the TCP connectionIt hasn't been released yet
. Must pass the time set by the time wait timer2MSL
Then, A can enter the CLOSED state.
The time MSL is called the maximum packet segment life, and RFC793 recommends setting it at two minutes. But two minutes is too long in modern engineering, so TCP allows different implementations to use smaller MSL values depending on the situation.
Code implementation
Start by creating two directories, one isclient
The client, the other one isserver
The service side.
1. The connection
1.1 the service side
- Listening to the connection
Net provides the Listen method, so that the server port listening
ADDRESS := "127.0.0.1:5000"
listener,err := net.Listen("tcp",ADDRESS)
iferr ! =nil {
fmt.Printf("start tcp server %s failed ,err : %s ",listener,err)
return
}
defer listener.Close()
Copy the code
1.2 the client
- Establish a connection
Net provides Dail methods that let clients connect to servers
ADDRESS := "127.0.0.1:5000"
conn,err := net.Dial("tcp",ADDRESS) // Initiate a connection with the server
iferr ! =nil {
fmt.Printf("dial %s failed; err :%s",ADDRESS,err)
return
}
Copy the code
2. Communication
2.1 the service side
- Receiving information
The transmitted data can be Read by.read.
var data [1024]byte
var msg string
reader := bufio.NewReader(os.Stdin)
for { // The server is always waiting for incoming data, so use the for loop
// Receive information
n,err := conn.Read(data[:])
if err == io.EOF{
break
}
iferr ! =nil {
fmt.Printf("read from conn failed,err:%s",err)
return
}
fmt.Println("Access Info : ".string(data[:n]))
}
defer conn.Close()
Copy the code
2.2 the client
- Send a message
It is also possible to transfer data over a transport connection via.write.
for{ // Let the client keep sending messages, so we need a for loop to keep the connection going
fmt.Print("Please enter:")
msg,_ = reader.ReadString('\n')
msg = strings.TrimSpace(msg)
if msg == "exit" {
break
}
_, _ = conn.Write([]byte(msg))
}
Copy the code
3. Reply
When the server receives the message, it should return the message to the client. Indicates that data has been received.
3.1 the service side
The server replies the message
// Reply to the message
fmt.Print("Reply message :")
msg,_ = reader.ReadString('\n')
msg = strings.TrimSpace(msg)
if msg == "exit" {
break
}
_ ,_ = conn.Write([]byte(msg))
Copy the code
3.2 the client
The client received the message. Procedure
// Receive information
n,err:=conn.Read(data[:])
if err == io.EOF {
break
}
iferr ! =nil {
fmt.Println("read from conn failed, err :",err)
return
}
fmt.Println("Reply received :".string(data[:n]))
Copy the code