node2:/root/test#cat t14.py
import socket
import struct
import time
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0))
s.connect(("192.168.137.2",8080))
s.send('4444')
s.recv(900)
s.close()
node1:/root/test#cat t2.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()
try:
msg = conn.recv(BUFSIZE) #接受消息的内容
print 'xxx--------------------xxx'
print msg
print 'xxx--------------------xxx'
except Exception,err:
break
if len(msg)==0:break #如果 不加,已连接的客户端突然断开,recv不再阻塞,发生死循环
print '111-----------------------111'
print msg
print '111-----------------------111'
time.sleep(2)
try:
conn.send('response:'+msg)
except Exception,err:
print err
#conn.send(msg.upper())#服务端发送消息
conn.close()#关闭链接
s.close()#关闭套接字
02:08:50.820763 IP node2.56683 > node1.webcache: Flags [S], seq 4188428377, win 2560, options [mss 256,sackOK,TS val 228065190 ecr 0,nop,wscale 7], length 0
02:08:50.822749 IP node1.webcache > node2.56683: Flags [S.], seq 3144228972, ack 4188428378, win 1708, options [mss 256,sackOK,TS val 228168524 ecr 228065190,nop,wscale 6], length 0
02:08:50.823429 IP node2.56683 > node1.webcache: Flags [.], ack 3144228973, win 20, options [nop,nop,TS val 228065192 ecr 228168524], length 0
发送数据长度4:
02:08:50.824408 IP node2.56683 > node1.webcache: Flags [P.], seq 4188428378:4188428382, ack 3144228973, win 20, options [nop,nop,TS val 228065193 ecr 228168524], length 4
02:08:50.825350 IP node1.webcache > node2.56683: Flags [.], ack 4188428382, win 27, options [nop,nop,TS val 228168528 ecr 228065193], length 0
返回数据长度13:
02:08:52.827214 IP node1.webcache > node2.56683: Flags [P.], seq 3144228973:3144228986, ack 4188428382, win 27, options [nop,nop,TS val 228170531 ecr 228065193], length 13
02:08:52.827280 IP node2.56683 > node1.webcache: Flags [.], ack 3144228986, win 20, options [nop,nop,TS val 228067196 ecr 228170531], length 0
02:08:52.827365 IP node2.56683 > node1.webcache: Flags [F.], seq 4188428382, ack 3144228986, win 20, options [nop,nop,TS val 228067196 ecr 228170531], length 0
02:08:52.827707 IP node1.webcache > node2.56683: Flags [F.], seq 3144228986, ack 4188428383, win 27, options [nop,nop,TS val 228170531 ecr 228067196], length 0
02:08:52.827725 IP node2.56683 > node1.webcache: Flags [.], ack 3144228987, win 20, options [nop,nop,TS val 228067197 ecr 228170531], length 0