TCP三次握手 TCP(Transmission Control Protocol) 传输控制协议 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接。 位码即tcp标志位,有6种标示: SYN(synchronous建立联机) ACK(acknowledgement 确认) PSH(push传送) FIN(finish结束) RST(reset重置) URG(urgent紧急)
Sequence number(顺序号码) Acknowledge number(确认号码) 第一次握手:主机A发送位码为syn=1,随机产生seq number=1234567的数据包到服务器,主机B由SYN=1知道,A要求建立联机; 第二次握手:主机B收到请求后要确认联机信息,向A发送ack number=(主机A的seq+1),syn=1,ack=1,随机产生seq=7654321的包 第三次握手:主机A收到后检查ack number是否正确,即第一次发送的seq number+1,以及位码ack是否为1,若正确,主机A会再发送ack number=(主机B的seq+1),ack=1,主机B收到后确认seq值与ack=1则连接建立成功。 完成三次握手,主机A与主机B开始传送数据。
在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接。 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认; 第二次握手:服务器收到syn包,必须确认客户的SYN(ack=j+1),同时自己也发送一个SYN包(syn=k),即SYN+ACK包,此时服务器进入SYN_RECV状态; 第三次握手:客户端收到服务器的SYN+ACK包,向服务器发送确认包ACK(ack=k+1),此包发送完毕,客户端和服务器进入ESTABLISHED状态,完成三次握手。 完成三次握手,客户端与服务器开始传送数据。
1.首先客户端通过向服务器端发送一个SYN来建立一个主动打开,作为三路握手的一部分。(同步位为1) 2. 然后服务器端应当为一个合法的SYN回送一个SYN/ACK。(同步位和确认位都为1) 3. 最后,客户端再发送一个ACK。这样就完成了三路握手,并进入了连接建立状态。(确认位位1)
建立连接协议(三次握手)
(1)客户端发送一个带SYN标志的TCP报文到服务器。这是三次握手过程中的报文1。 (2) 服务器端回应客户端的,这是三次握手中的第2个报文,这个报文同时带ACK标志和SYN标志。因此它表示对刚才客户端SYN报文的回应;同时又标志SYN给客户端,询问客户端是否准备好进行数据通讯。 (3) 客户必须再次回应服务段一个ACK报文,这是报文段3。
建立起一个TCP连接需要经过“三次握手”: 第一次握手:客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等待服务器确认; 第二次握手:服务器收到syn包,必须确认客户的SYN(ack=j+1),同时自己也发送一个SYN包(syn=k),即SYN+ACK包,此时服务器进入SYN_RECV状态; 第三次握手:客户端收到服务器的SYN+ACK包,向服务器发送确认包ACK(ack=k+1),此包发送完毕,客户端和服务器进入ESTABLISHED状态,完成三次握手。 握手过程中传送的包里不包含数据,三次握手完毕后,客户端与服务器才正式开始传送数据。理想状态下,TCP连接一旦建立,在通信双方中的任何一方主 动关闭连接之前,TCP 连接都将被一直保持下去。断开连接时服务器和客户端均可以主动发起断开TCP连接的请求,断开过程需要经过“四次握手”。
笔试题:tcp三次握手的过程,accept发生在三次握手哪个阶段? 分析: accept发生在三次握手之后。 第一次握手:客户端发送syn包(syn=j)到服务器。 第二次握手:服务器收到syn包,必须确认客户的SYN(ack=j+1),同时自己也发送一个ASK包(ask=k)。 第三次握手:客户端收到服务器的SYN+ACK包,向服务器发送确认包ACK(ack=k+1)。 三次握手完成后,客户端和服务器就建立了tcp连接。这时可以调用accept函数获得此连接。
TCP握手协议 在TCP/IP协议中,TCP协议提供可靠的衔接服务,采用三次握手树立一个衔接。 第一次握手:树立衔接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND形态,等候服务器确认; SYN: 同步序列编号(Synchronize Sequence Numbers) 第二次握手:服务器收到syn包,必需确认客户的SYN(ack=j+1),同时自己也发送一个SYN包(syn=k),即SYN+ACK包,此时服务器进入SYN_RECV形态; 第三次握手:客户端收到服务器的SYN+ACK包,向服务器发送确认包ACK(ack=k+1),此包发送终了,客户端和服务器进入ESTABLISHED形态,完成三次握手。
一,TCP建立连接(Connection Establishment)由以下三个过程: 1)Client给Server发送发送TCP segment:SYN=1,ACK=0,SEQ=x。(这些字段的作用是这样的:通过SYN=1,ACK=0告诉server这是连接请求Connection Requet。SEQ=x表明后面传送数据时的第一个数据字节的序号是x+1) 2)Server给Client发送的TCP segment:SYN=1,ACK=1, SEQ = y, ACK(ackonwlege number)=x+1.(server通过SYN=1,ACK=1告诉client,这是接受请求Connection Accepted。确认你是要从x+1传起了。server也通知client,我要传给你的是从y+1传起的哦!) 3) Client给Server发送发送TCP segment: ACK=1, SEQ=x+1, ACk(acknowledeg number)=y+1(client告诉server,OK,我是要建立连接,你可以建立了) 说明:1)为什么需要第三个步骤呢?Internet是不可靠的,有时候由于网络延迟,Client可能会“重传”第一个请求连接的报文。如果Server每次收到Connetion Request(也就是SYN=1,ACK=0)的报文就建立连接,那Server就亏大了(浪费资源)。所以需要Client再确认一下,免得 Server吃亏。 2) 在第一个步骤,如果Server拒绝连接,就可以设置RST=1,并回复Client。 3)如果Client在第一个步骤发送的数据报的时候,源IP随便填写(这并不影响路由过程),这样,Server收到连接请求(SYN=1,ACK= 0),并不会马上为这个连接请求Client建立连接,而是为连接请求建立会话,并放到等待队列中,向源IP主机发送确认数据包,并等待回复。由于 Server收不到第三个步骤的回复确认(当然了,源IP是假的),会一直等待直到超时,当有大量这样的连接时,Server就无法接受新的连接请求了,这就是“DOS(denial of Service)攻击”的原理. 二 WIN+R打开运行对话框。输入powershell打开。执行netstat命令,-o参数可显示端口对应的pid。执行netstat命令,-ao -p udp,-a即所有端口,-p指明使用的协议
三 1.R1 sys system-view [Huawei]sysn R1 [R1]user-interface console 0 [R1-ui-console0]idle-timeout 0 [R1-ui-console0]q [R1]int gi 0/0/0 [R1-GigabitEthernet0/0/0]ip address 192.168.10.1 24 Jul 5 2021 17:22:18-08:00 R1 %%01IFNET/4/LINK_STATE(l)[0]:The line protocol IP on the interface GigabitEthernet0/0/0 has entered the UP state. [R1-GigabitEthernet0/0/0] [R1-GigabitEthernet0/0/0]q [R1]int loo 0 [R1-LoopBack0] [R1-LoopBack0]ip ad [R1-LoopBack0]ip address 1.1.1.1 24 [R1-LoopBack0]int loo 1 [R1-LoopBack1]ip ad [R1-LoopBack1]ip address 1.1.2.1 24 [R1-LoopBack1]int gi 0/0/1 [R1-GigabitEthernet0/0/1]ip ad [R1-GigabitEthernet0/0/1]ip address 192.168.40.2 24 Jul 5 2021 17:31:47-08:00 R1 %%01IFNET/4/LINK_STATE(l)[1]:The line protocol IP on the interface GigabitEthernet0/0/1 has entered the UP state. [R1-GigabitEthernet0/0/1]q [R1]ip route-static 192.168.20.0 24 192.168.10.2 [R1]ip route-static 2.2.1.0 24 192.168.10.2 [R1]ip route-static 2.2.2.0 24 192.168.10.2 [R1]ip route-static 192.168.30.0 24 192.168.40.1 [R1]ip route-static 192.168.100.0 24 192.168.40.1 [R1]ip route-static 192.168.50.0 24 192.168.40.1 [R1]user-interface vty 0 4 [R1-ui-vty0-4]set authentication password cipher 123456 [R1]ping 192.168.50.1 PING 192.168.50.1: 56 data bytes, press CTRL_C to break Request time out Reply from 192.168.50.1: bytes=56 Sequence=2 ttl=254 time=50 ms Reply from 192.168.50.1: bytes=56 Sequence=3 ttl=254 time=40 ms Reply from 192.168.50.1: bytes=56 Sequence=4 ttl=254 time=20 ms Reply from 192.168.50.1: bytes=56 Sequence=5 ttl=254 time=30 ms
— 192.168.50.1 ping statistics — 5 packet(s) transmitted 4 packet(s) received 20.00% packet loss round-trip min/avg/max = 20/35/50 ms
[R1]ip route-static 192.168.60.0 24 192.168.10.2 [R1] R2
sysname R2
snmp-agent local-engineid 800007DB03000000000000 snmp-agent
clock timezone China-Standard-Time minus 08:00:00
portal local-server load portalpage.zip
drop illegal-mac alarm
set cpu-usage threshold 80 restore 75
aaa authentication-scheme default authorization-scheme default accounting-scheme default domain default domain default_admin local-user admin password cipher %
%
K8m.Nt84DZ}e#<0`8bmE3Uw}%
%
local-user admin service-type http
firewall zone Local priority 15
interface GigabitEthernet0/0/0 ip address 192.168.20.1 255.255.255.0
interface GigabitEthernet0/0/1 ip address 192.168.10.2 255.255.255.0
interface GigabitEthernet0/0/2
interface NULL0
interface LoopBack0 ip address 2.2.1.1 255.255.255.0
interface LoopBack1 ip address 2.2.2.1 255.255.255.0
ip route-static 1.1.1.0 255.255.255.0 192.168.10.1 ip route-static 1.1.2.0 255.255.255.0 192.168.10.1 ip route-static 192.168.30.0 255.255.255.0 192.168.10.1 ip route-static 192.168.40.0 255.255.255.0 192.168.10.1 ip route-static 192.168.50.0 255.255.255.0 192.168.20.2 ip route-static 192.168.60.0 255.255.255.0 192.168.20.2 ip route-static 192.168.100.0 255.255.255.0 192.168.10.1 2.R3
system-view Enter system view, return user view with Ctrl+Z. [Huawei]sysn R3 [R3]user-interface console 0 [R3-ui-console0]id [R3-ui-console0]idle-timeout 0 [R3-ui-console0] [R3-ui-console0]q [R3]int gi 0/0/1 [R3-GigabitEthernet0/0/1]ip ad [R3-GigabitEthernet0/0/1]ip address 192.168.20.2 24 Jul 5 2021 17:26:13-08:00 R3 %%01IFNET/4/LINK_STATE(l)[0]:The line protocol IP on the interface GigabitEthernet0/0/1 has entered the UP state. [R3-GigabitEthernet0/0/1]int gi 0/0/0 [R3-GigabitEthernet0/0/0]ip ad [R3-GigabitEthernet0/0/0]ip address 192.168.30.1 24 Jul 5 2021 17:26:30-08:00 R3 %%01IFNET/4/LINK_STATE(l)[1]:The line protocol IP on the interface GigabitEthernet0/0/0 has entered the UP state. [R3-GigabitEthernet0/0/0]int loo 0 [R3-LoopBack0]ip ad [R3-LoopBack0]ip address 3.3.1.1 24 [R3-LoopBack0]int loo 1 [R3-LoopBack1]ip ad [R3-LoopBack1]ip address 3.3.2.1 24 [R3-LoopBack1]int gi 0/0/2 [R3-GigabitEthernet0/0/2]ip ad [R3-GigabitEthernet0/0/2]ip address 192.168.50.1 24 [R3-GigabitEthernet0/0/2] Jul 5 2021 17:33:45-08:00 R3 %%01IFNET/4/LINK_STATE(l)[2]:The line protocol IP on the interface GigabitEthernet0/0/2 has entered the UP state. [R3-GigabitEthernet0/0/2]q [R3]ip ro [R3]ip route-s [R3]ip route-static 192.168.10.0 24 192.168.20.1 [R3]ip route-static 192.168.40.0 24 192.168.20.1 [R3]ip route-static 192.168.100.0 24 192.168.20.1 [R3]ip route-static 1.1.1.0 24 192.168.20.1 [R3]ip route-static 1.1.2.0 24 192.168.20.1 [R3]ip route-static 2.2.1.0 24 192.168.20.1 [R3]ip route-static 2.2.2.0 24 192.168.20.1 [R3]ip route-static 192.168.50.0 24 192.168.20.1 [R3]ip route-static 192.168.50.0 24 192.168.20.1 p [R3]display ip [R3]display ip ro [R3]display ip routing-table Route Flags: R - relay, D - download to fib
Routing Tables: Public Destinations : 26 Routes : 26
Destination/Mask Proto Pre Cost Flags NextHop Interface
1.1.1.0/24 Static 60 0 RD 192.168.20.1 GigabitEthernet0/0/1
1.1.2.0/24 Static 60 0 RD 192.168.20.1 GigabitEthernet0/0/1
2.2.1.0/24 Static 60 0 RD 192.168.20.1 GigabitEthernet0/0/1
2.2.2.0/24 Static 60 0 RD 192.168.20.1 GigabitEthernet0/0/1
3.3.1.0/24 Direct 0 0 D 3.3.1.1 LoopBack0
3.3.1.1/32 Direct 0 0 D 127.0.0.1 LoopBack0
3.3.1.255/32 Direct 0 0 D 127.0.0.1 LoopBack0
3.3.2.0/24 Direct 0 0 D 3.3.2.1 LoopBack1
3.3.2.1/32 Direct 0 0 D 127.0.0.1 LoopBack1
3.3.2.255/32 Direct 0 0 D 127.0.0.1 LoopBack1
127.0.0.0/8 Direct 0 0 D 127.0.0.1 InLoopBack0
127.0.0.1/32 Direct 0 0 D 127.0.0.1 InLoopBack0
127.255.255.255/32 Direct 0 0 D 127.0.0.1 InLoopBack0 192.168.10.0/24 Static 60 0 RD 192.168.20.1 GigabitEthernet0/0/1 192.168.20.0/24 Direct 0 0 D 192.168.20.2 GigabitEthernet0/0/1 192.168.20.2/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/1 192.168.20.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/1 192.168.30.0/24 Direct 0 0 D 192.168.30.1 GigabitEthernet0/0/0 192.168.30.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/0 192.168.30.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/0 192.168.40.0/24 Static 60 0 RD 192.168.20.1 GigabitEthernet0/0/1 192.168.50.0/24 Direct 0 0 D 192.168.50.1 GigabitEthernet0/0/2 192.168.50.1/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/2 192.168.50.255/32 Direct 0 0 D 127.0.0.1 GigabitEthernet0/0/2 [R3]ip route-static 192.168.60.0 24 192.168.50.2 preference 61 Info: Succeeded in modifying route. [R3][R3]ip route-static 192.168.60.0 24 192.1 R4
sys system-view Enter system view, return user view with Ctrl+Z. [Huawei]sysn R4 [R4]int gi 0/0/1 [R4-GigabitEthernet0/0/1]ip ad [R4-GigabitEthernet0/0/1]ip address 192.168.30.2 24 Jul 5 2021 17:29:55-08:00 R4 %%01IFNET/4/LINK_STATE(l)[0]:The line protocol IP on the interface GigabitEthernet0/0/1 has entered the UP state. [R4-GigabitEthernet0/0/1]int gi 0/0/0 [R4-GigabitEthernet0/0/0]ip ad [R4-GigabitEthernet0/0/0]ip address 192.168.40.1 24 [R4-GigabitEthernet0/0/0] Jul 5 2021 17:30:13-08:00 R4 %%01IFNET/4/LINK_STATE(l)[1]:The line protocol IP on the interface GigabitEthernet0/0/0 has entered the UP state. [R4-GigabitEthernet0/0/0]int loo 0 [R4-LoopBack0]ip ad [R4-LoopBack0]ip address 4.4.1.1 24 [R4-LoopBack0]int loo 1 [R4-LoopBack1]ip ad [R4-LoopBack1]ip address 4.4.2.1 24 [R4-LoopBack1]
Please check whether system data has been changed, and save data in time
Configuration console time out, please press any key to log on
int gi 0/0/2 ^ Error: Unrecognized command found at ‘^’ position. sys system-view Enter system view, return user view with Ctrl+Z. [R4]int gi0/0/2 [R4-GigabitEthernet0/0/2]ip ad [R4-GigabitEthernet0/0/2]ip address 192.168.100.1 24 [R4-GigabitEthernet0/0/2] Jul 5 2021 17:40:31-08:00 R4 %%01IFNET/4/LINK_STATE(l)[0]:The line protocol IP on the interface GigabitEthernet0/0/2 has entered the UP state. [R4-GigabitEthernet0/0/2]
Please check whether system data has been changed, and save data in time
Configuration console time out, please press any key to log on
<R4 sts sys system-view Enter system view, return user view with Ctrl+Z. [R4]ip ro [R4]ip route-st [R4]ip route-static 192.168.10.0 24 192.168.40.2 [R4]ip route-static 192.168.20.0 24 192.168.40.2 [R4]ip route-static 1.1.1.0 24 192.168.40.2 [R4]ip route-static 1.1.2.0 24 192.168.40.2 [R4]ip route-static 2.2.2.0 24 192.168.40.2 [R4]ip route-static 2.2.1.0 24 192.168.40.2 [R4] [R4]int gi 0/0/2 [R4-GigabitEthernet0/0/2]q [R4]dh [R4]dhcpser [R4]dhcp se [R4]dhcp server ^ Error:Incomplete command found at ‘^’ position. [R4]dh [R4]dhcp en [R4]dhcp enable Info: The operation may take a few seconds. Please wait for a moment.done. [R4]int gi [R4]int GigabitEthernet 0/0/2 [R4-GigabitEthernet0/0/2]dhcp select interface [R4-GigabitEthernet0/0/2]dhcp server dns-list 8.8.8.8 [R4]ip ro [R4]ip route st [R4]ip route-s [R4]ip route-static 192.168.50.0 24 192.168.40.2 [R4]
Please check whether system data has been changed, and save data in time
Configuration console time out, please press any key to log on
[R4]ip route-static 192.168.60.0 24 192.168.30.1 [R4]ping 192.168.60.1 PING 192.168.60.1: 56 data bytes, press CTRL_C to break Reply from 192.168.60.1: bytes=56 Sequence=1 ttl=254 time=40 ms Reply from 192.168.60.1: bytes=56 Sequence=2 ttl=254 time=30 ms Reply from 192.168.60.1: bytes=56 Sequence=3 ttl=254 time=20 ms Reply from 192.168.60.1: bytes=56 Sequence=4 ttl=254 time=30 ms Reply from 192.168.60.1: bytes=56 Sequence=5 ttl=254 time=30 ms
— 192.168.60.1 ping statistics — 5 packet(s) transmitted 5 packet(s) received 0.00% packet loss round-trip min/avg/max = 20/30/40 ms
r5 sys system-view Enter system view, return user view with Ctrl+Z. [Huawei]sysn R5 [R5]user-i [R5]user-interface co [R5]user-interface console 0 [R5-ui-console0]id [R5-ui-console0]idle-timeout 0 [R5-ui-console0] [R5-ui-console0]q [R5]int gi 0/0/0 [R5-GigabitEthernet0/0/0]ip ad [R5-GigabitEthernet0/0/0]ip address 192.168.50.2 24 Jul 5 2021 17:34:16-08:00 R5 %%01IFNET/4/LINK_STATE(l)[0]:The line protocol IP on the interface GigabitEthernet0/0/0 has entered the UP state. [R5-GigabitEthernet0/0/0]]int gi0/0/1 ^ Error: Unrecognized command found at '^' position. [R5-GigabitEthernet0/0/0]ip ad [R5-GigabitEthernet0/0/0]ip address 192.168 ^ Error: Wrong parameter found at '^' position. [R5-GigabitEthernet0/0/0] [R5-GigabitEthernet0/0/0]ip ro [R5-GigabitEthernet0/0/0]ip ro-s [R5-GigabitEthernet0/0/0]q [R5]ip ro [R5]ip route-s [R5]ip route-static 192.168.10.0 24 192.168.50.1 [R5]ip route-static 192.168.20.0 24 192.168.50.1 [R5]ip route-static 192.168.30.0 24 192.168.50.1 [R5]ip route-static 192.168.40.0 24 192.168.50.1 [R5]ip route-static 1.1.1.0 24 192.168.50.1 [R5]ip route-static 1.1.2.0 24 192.168.50.1 [R5]ip route-static 2.2.2.0 24 192.168.50.1 [R5]ip route-static 2.2.1.0 24 192.168.50.1 [R5]ip route-static 192.168.100.0 24 192.168.50.1 [R5]int gi 0/0/1 [R5-GigabitEthernet0/0/1]ip nad [R5-GigabitEthernet0/0/1]ip ad [R5-GigabitEthernet0/0/1]ip address 192.168.60.1 24 Jul 5 2021 18:14:38-08:00 R5 %%01IFNET/4/LINK_STATE(l)[1]:The line protocol IP on the interface GigabitEthernet0/0/1 has entered the UP state. [R5-GigabitEthernet0/0/1]ip ro [R5-GigabitEthernet0/0/1]q [R5]ip route-static 0.0.0.0 0.0.0.0 170.1.1.1 [R5]
R6 sys system-view Enter system view, return user view with Ctrl+Z. [Huawei] [Huawei]sysn R6 [R6]int gi 0/0/0 [R6-GigabitEthernet0/0/0]ip ad [R6-GigabitEthernet0/0/0]ip address 192.168.60.2 24 Jul 5 2021 18:16:12-08:00 R6 %%01IFNET/4/LINK_STATE(l)[0]:The line protocol IP on the interface GigabitEthernet0/0/0 has entered the UP state. [R6-GigabitEthernet0/0/0]int gi 0/0/1 [R6-GigabitEthernet0/0/1]ip ad [R6-GigabitEthernet0/0/1]ip address 170.1.1.1 24 [R6-GigabitEthernet0/0/1]q [R6-GigabitEthernet0/0/1]nat server protocol tcp global 192.168.60.1 inside 192.168.10.1
|