服务器段代码
__author__ = 'Administrator'
import socket,time
host="localhost"
port=9090
addr=(host,port)
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind(addr)
server.listen(6)
while True:
client_conn,addr=server.accept()
while True:
filename=client_conn.recv(1024).decode("utf-8")
print(filename)
f=open(filename,"r",encoding="utf-8")
filetxt=""
for line in f.readlines():
filetxt+=line
f.close()
print(filetxt)
filesize=str(len(filetxt))
client_conn.send(filesize.encode("utf-8"))
time.sleep(0.5)
client_conn.send(filetxt.encode("utf-8"))
server.close()
客户端代码:
__author__ = 'Administrator'
import socket
host="localhost"
port=9090
addr=(host,port)
client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect(addr)
while True:
filename=input(">>:").strip().encode("utf-8")
client.send(filename)
s_size=int(client.recv(1024).decode())
recive_size=0
date=""
while recive_size<s_size:
date+=date
date=client.recv(1024).decode("utf-8")
recive_size+=len(date)
print(s_size,recive_size)
print(date)