Quick-Know TCP
- 全双工
- 点对点连接
MSS
(Maximum Segment Size) MSS=MTU-TCP_Header-IP_Header TCP payload
MTU
(Maximum Transmission Unit) 最大链路层帧长度 data-link payloadTCP Segment=message+TCP header
连接
建立
三路握手
sequenceDiagram
participant c as client
participant s as server
c->>s:SYN(TCP req) seq=x
Note over c:SYN-SENT
s->>c:SYN ACK seq=y ack=x+1
Note over s:SYN-RECV
c->>s:ACK seq=x+1 ack=y+1
Note over c:ESTABLISHED
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
拆除
四路挥手
sequenceDiagram
participant c as client
participant s as server
c->>s:FIN ACK
Note over c:FIN-WAIT-1
s->>c:ACK
Note over c:FIN-WAIT-2
s->>c:FIN ACK
Note over s:CLOSE-FIN
c->>s:ACK
Note over s:LAST-ACK
Note over c:wait 2MSL
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
流量控制
维护接收窗口 消除发送方使接收方缓存溢出的可能性
TCP ZeroWindow Probe
当接收方缓存满时 回复 Window Full
ACK 之后发送方会间断发送 Len为0的 Keep-Alive
接收方会回复 ZeroWindow
拥塞控制
维护拥塞窗口(发送窗口) 防止网络拥塞
网络拥塞信号
- 超时
- 3个冗余ACK
为什么不是两个冗余ACK? 两个大概率是乱序 三个更可能是丢包
编程
服务端代码
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
void worker(int client){
// 向客户端发送数据
char str[] = "Hello World!";
write(client, str, sizeof(str));
// 关闭套接字
close(client);
}
void serve() {
const int port = 12345;
int serv_sock = socket(AF_INET, SOCK_STREAM, 0); // tcp server socket
// 将套接字和IP、端口绑定
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr)); // 每个字节都用0填充
serv_addr.sin_family = AF_INET; // 使用IPv4地址
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 具体的IP地址
serv_addr.sin_port = htons(port); // 端口
bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
// 进入监听状态,等待用户发起请求
listen(serv_sock, 20);
// 接收客户端请求
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size = sizeof(clnt_addr);
int clnt_sock =
accept(serv_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
worker(clnt_sock);
// 关闭套接字
close(serv_sock);
}
int main() {
//创建套接字
serve();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
客户端代码
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
int main() {
const int port = 12345;
//创建套接字
int sock = socket(AF_INET, SOCK_STREAM, 0);
//向服务器(特定的IP和端口)发起请求
struct sockaddr_in serv_addr;
memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充
serv_addr.sin_family = AF_INET; //使用IPv4地址
serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址
serv_addr.sin_port = htons(port); //端口
connect(sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
//读取服务器传回的数据
char buffer[40];
read(sock, buffer, sizeof(buffer) - 1);
printf("Message form server: %s\n", buffer);
//关闭套接字
close(sock);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Makefile
go:client server
./server&
./client
client:client.c
server:server.c
clean:
rm client server
1
2
3
4
5
6
7
2
3
4
5
6
7