标签:绑定 python bind mes buffer 服务 nec add 绑定ip
套接字相关的系统调用:socket通信示例:
服务器端tcpserver.py
#/usr/bin/python
import socket
HOST=‘127.0.0.1‘
PORT=9527
BUFFER=4096
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind((HOST,PORT))
sock.listen(3)
print(‘tcpServer listen at: %s:%s\n\r‘ %(HOST,PORT))
while True:
client_sock,client_addr=sock.accept()
print(‘%s:%s connect‘ %client_addr)
while True:
recv=client_sock.recv(BUFFER)
if not recv:
client_sock.close()
break
print(‘[Client %s:%s said]:%s‘ %(client_addr[0],client_addr[1],recv))
client_sock.send(‘tcpServer has received your message‘)
sock.close()
客户端tcpclient
#/usr/bin/python
import socket
HOST=‘127.0.0.1‘
PORT=9527
BUFFER=4096
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.connect((HOST,PORT))
sock.send(‘hello, tcpServer!‘)
recv=sock.recv(BUFFER)
print(‘[tcpServer said]: %s‘ % recv)
sock.close()
执行下面命令测试
python tcpserver
phthon tcpclient
标签:绑定 python bind mes buffer 服务 nec add 绑定ip
原文地址:http://blog.51cto.com/11034229/2064818