创建TCP服务器代码,
#coding = utf-8
from socket import *
from time import ctime
HOST=''
PORT=21567
BUFSIZ=1024
ADDR=(HOST, PORT)
tcpSerSock=socket(AF_INET, SOCK_STREAM) #分配了TCP服务器套接字(tcpSerSock),紧随其后的是将套接字绑定到服务器地址以及开启TCP监听器的调用。
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
print('waiting for connection,,,')
tcpCliSock, addr=tcpSerSock.accept()
print('...connected from:', addr)
while True:
data=tcpCliSock.recv(BUFSIZ).decode()
print('date=', data)
if not data:
break
tcpCliSock.send(( '[%s] %s' % (ctime(), data)).encode())
tcpCliSock.close()
tcpCliSock.close()
创建TCP客户端,
#coding = utf-8
from socket import *
HOST='127.0.0.1'
PORT = 21567
BUFSIZ= 1024
ADDR=(HOST, PORT)
tcpCl