一个TCP连接上,如果通信双方都不向对方发送数据,那么TCP
连接就不会有任何数据交换,这就是我们今天要讲的TCP keepalive机制的由来
永远记住TCP 不是轮训的协议
客户端发出三次握手以后故障宕机或被踢掉网线,对于 web 服务器而已,下一个数据包将永远无法到来,
但是它一无所知。TCP 不会采用类似于轮询的方式来询问:
小老弟你有什么东西要发给我吗?
TCP 的 half open
node1:/root/test#cat t200.py
# coding = utf-8
# -*- coding:utf-8 -*-
import socket
import time
ip_port = ('192.168.137.2',8080)
BUFSIZE = 9000
SEND_BUF_SIZE = 2
RECV_BUF_SIZE = 1
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#创建套接字
print("Buffer size [Before]: %d" % s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF))
print("Buffer size [Before]: %d" % s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt( socket.SOL_SOCKET, socket.SO_SNDBUF, SEND_BUF_SIZE)
s.setsockopt( socket.SOL_SOCKET, socket.SO_RCVBUF, RECV_BUF_SIZE)
print 'alter----------------';
print s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
print s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
#s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 0)
s.bind(ip_port)#绑定地址
s.listen(5)#监听链接
print('server listening8080...')
while True: #无限等待连接
conn,addr = s.accept() #接受客户端连接
print(conn)
print(addr)
print('接到来自%s的链接'%addr[0])
while True: #通信循环,无限接受客户端信息
#print conn.getpeername()
msg = conn.recv(BUFSIZE) #接受消息的内容
print 'xxx--------------------xxx'
print msg
print 'xxx--------------------xxx'
if len(msg)==0:break #如果 不加,已连接的客户端突然断开,recv不再阻塞,发生死循环
print '111-----------------------111'
print msg
print '111-----------------------111'
#time.sleep(13.6)
conn.send('response:'+str(len(msg)))
#conn.send(msg.upper())#服务端发送消息
conn.close()#关闭链接
s.close()#关闭套接字
node1:/root/test#netstat -na | grep 8080
tcp 0 0 192.168.137.2:8080 0.0.0.0:* LISTEN
tcp 0 0 192.168.137.2:8080 192.168.137.3:52175 ESTABLISHED
node1:/root/test#
node2:/root#netstat -na | grep 8080
tcp 0 0 192.168.137.3:52175 192.168.137.2:8080 ESTABLISHED
node2:/root#
此时断开node1的网络:
发现TCP长连接还在:
node2:/root#netstat -na | grep 8080
tcp 0 15 192.168.137.3:52175 192.168.137.2:8080 ESTABLISHED
node2:/root/test#python connect1.py
Traceback (most recent call last):
File "connect1.py", line 10, in <module>
print s.recv(90)
socket.timeout: timed out
You have mail in /var/spool/mail/root
node2:/root/test#vim connect1.py
node2:/root/test#time python connect1.py
Traceback (most recent call last):
File "connect1.py", line 10, in <module>
print s.recv(90)
socket.timeout: timed out
real 1m50.085s
user 0m0.021s
sys 0m0.005s
node2:/root/test#cat connect1.py
# -*- coding: utf-8 -*-
import socket
import time
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.settimeout(20)
s.connect(("192.168.137.2",8080))
time.sleep(90)
s.send('aaaaaaaaaaaaaaa')
print s.recv(90)
node2:/root#netstat -na | grep 8080
tcp 0 15 192.168.137.3:52175 192.168.137.2:8080 ESTABLISHED
You have new mail in /var/spool/mail/root
node2:/root#netstat -na | grep 8080
tcp 0 16 192.168.137.3:52175 192.168.137.2:8080 FIN_WAIT1
node2:/root#
node2:/root#
node2:/root#netstat -na | grep 8080
tcp 0 16 192.168.137.3:52175 192.168.137.2:8080 FIN_WAIT1
node2:/root#netstat -na | grep 8080
node1:/root#cat 8080.txt
tcp 0 0 192.168.137.2:8080 0.0.0.0:* LISTEN
tcp 0 0 192.168.137.2:8080 192.168.137.3:52176 ESTABLISHED
tcp 0 0 192.168.137.2:8080 192.168.137.3:52175 ESTABLISHED