node1:/root/test#cat t2.py
# coding = utf-8
# -*- coding:utf-8 -*-
import socket
ip_port = ('192.168.137.2',8080)
BUFSIZE = 60
SEND_BUF_SIZE = 500
RECV_BUF_SIZE = 14
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_SNDBUF, SEND_BUF_SIZE)
s.setsockopt( socket.SOL_SOCKET, socket.SO_RCVBUF, RECV_BUF_SIZE)
print s.getsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF)
print s.getsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF)
#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:
peeraddr=conn.getpeername()
msg = conn.recv(BUFSIZE) #接受消息的内容
except:
break;
if len(msg)==0:break #如果 不加,已连接的客户端突然断开,recv不再阻塞,发生死循环
print '-----------------------'
print msg
print '-----------------------'
try:
conn.send('response:'+msg)
except:
print '555555555'
#conn.send(msg.upper())#服务端发送消息
conn.close()#关闭链接
s.close()#关闭套接字
服务端buffer最大限制:
-----------------------
111111111111111111111111111111111111111111111111111111111111
-----------------------
通过这个参数conn.recv(BUFSIZE) #接受消息的内容