我试图在Python中创建一个实现套接字的程序,每个客户端发送一个PDF文件,服务器接收它,标题更改为&#34; file_number.pdf&#34; (即:file_1.pdf)。提出的问题是只有客户端才能成功发送文件。当第二个客户端尝试发送程序时崩溃。我做错了什么以及如何解决我的代码以允许N个客户端(N <20)连接到服务器并传输文件?
这是服务器代码:
import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10) # Acepta hasta 10 conexiones entrantes.
sc, address = s.accept()
print address
i=1
f = open('file_'+ str(i)+".pdf",'wb') # Open in binary
i=i+1
while (True):
# Recibimos y escribimos en el fichero
l = sc.recv(1024)
while (l):
f.write(l)
l = sc.recv(1024)
f.close()
sc.close()
s.close()
这是客户端代码:
import socket
import sys
s = socket.socket()
s.connect(("localhost",9999))
f = open ("libroR.pdf", "rb")
l = f.read(1024)
while (l):
s.send(l)
l = f.read(1024)
s.close()
为了简化我的代码,我总是使用一本文件名为#lib; libroR.pdf&#34;的书,但在完整的代码中,它是由图形用户界面选择的。
答案 0 :(得分:15)
您必须将sc, address = s.accept()
到sc.close()
的所有代码放到另一个循环中,否则服务器会在收到第一个文件后终止。它没有崩溃,脚本刚刚完成。
[编辑] 以下是修订后的代码:
import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10) # Accepts up to 10 connections.
while True:
sc, address = s.accept()
print address
i=1
f = open('file_'+ str(i)+".pdf",'wb') #open in binary
i=i+1
while (True):
# receive data and write it to file
l = sc.recv(1024)
while (l):
f.write(l)
l = sc.recv(1024)
f.close()
sc.close()
s.close()
请注意,s.listen(10)
表示“set maximum accept rate to 10 connections”,而不是“10次连接后停止”。
答案 1 :(得分:4)
你的代码在第二次循环中陷入困境。
请参阅:
import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10)
i=1
while True:
sc, address = s.accept()
print address
f = open('file_'+str(i)+".pdf",'wb') #open in binary
i=i+1
print(i)
l = 1
while(l):
l = sc.recv(1024)
while (l):
f.write(l)
l = sc.recv(1024)
f.close()
sc.close()
s.close()
答案 2 :(得分:1)
在处理第一个客户端连接后,您正在关闭服务器套接字(代码中为s
)。因此,您的服务器只处理一个客户端。围绕accept
循环并阅读sc
。
答案 3 :(得分:1)
服务器:
import socket
from threading import Thread
TCP_IP = 'localhost'
TCP_PORT = 9001
BUFFER_SIZE = 1024
class ClientThread(Thread):
def __init__(self, ip, port, sock):
Thread.__init__(self)
self.ip = ip
self.port = port
self.sock = sock
print(" New thread started for "+ip+":"+str(port))
def run(self):
filename = 'anon234.jpeg'
f = open(filename, 'rb')
while True:
l = f.read(BUFFER_SIZE)
while (l):
self.sock.send(l)
#print('Sent ',repr(l))
l = f.read(BUFFER_SIZE)
if not l:
f.close()
self.sock.close()
break
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcpsock.bind((TCP_IP, TCP_PORT))
threads = []
while True:
tcpsock.listen(5)
print("Waiting for incoming connections...")
(conn, (ip, port)) = tcpsock.accept()
print('Got connection from ', (ip, port))
newthread = ClientThread(ip, port, conn)
newthread.start()
threads.append(newthread)
for t in threads:
t.join()
客户:
import socket
import time
TCP_IP = 'localhost'
TCP_PORT = 9001
BUFFER_SIZE = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
recived_f = 'imgt_thread'+str(time.time()).split('.')[0]+'.jpeg'
with open(recived_f, 'wb') as f:
print('file opened')
while True:
#print('receiving data...')
data = s.recv(BUFFER_SIZE)
print('data=%s', (data))
if not data:
f.close()
print('file close()')
break
# write data to a file
f.write(data)
print('Successfully get the file')
s.close()
print('connection closed')
答案 4 :(得分:0)
使用此代码,您可以使用同一client.py
多次发送文件。Server.py
import socket
import sys
s = socket.socket()
s.bind(("localhost",9999))
s.listen(10) # Acepta hasta 10 conexiones entrantes.
i = 1
while True:
sc, address = s.accept()
print address
f = open('file_'+ str(i)+".wav",'wb') #open in binary
i=i+1
while (True):
# recibimos y escribimos en el fichero
l = sc.recv(1024)
f.write(l)
if not l:
break
f.close()
sc.close()
print('copied the file.')
s.close()