我的服务器向客户端发送文件时遇到了非常不寻常的行为(从它们接收没有问题)。更糟糕的是,它在看似随机的时候挂断了。有时传输100个文件,有时只传输1-3个文件。
它挂起的地方:在while循环中的“l = s.recv(512)”的客户端代码定义中的receiveFile
编辑:一些重要信息。当我在服务器上运行客户端代码时,它不会挂断!
很明显,客户端正在等待某种输入,服务器不再提供数据了?但为什么服务器功能不再提供数据呢?我显然错过了一些明显的东西?为什么它会随机挂断?
我正在尝试将4000个相对较小的文件从我的服务器传输到客户端。文件大小范围从981B到37.0kB。
我已尝试更改发送/接收的字节数,但没有任何影响。
我知道这是一个很高的命令,但任何帮助的尝试都非常感激(现在已经工作了一个星期但没有用)
服务器代码:
import socket
import os
import sys
import time
import thread
import easygui
import os.path
import MySQLdb
from socket import error as socket_error
from random import randint
serverIP = "192.168.1.65"
class fileConnection(object):
def connect(self, clientsocket):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
connected = False
while not connected:
try:
port = randint(1024,2024)
sock.bind((serverIP,port))
connected = True
except socket_error:
None
return sock
def receiveFile(self, fileName, clientsocket):
s = self.connect(clientsocket)
s.listen(5)
clientsocket.send(str(s.getsockname()[1]).ljust(5)[0:5])
c, addr = s.accept()
fout = open(fileName,'wb')
l = c.recv(512)
while (l):
fout.write(l)
l = c.recv(512)
fout.close()
s.shutdown(2) #has to be 2
#s.close() #is this causing problems?
def sendFile(self, fileName, clientsocket):
s = self.connect(clientsocket)
s.listen(5)
clientsocket.send(str(s.getsockname()[1]).ljust(5)[0:5])
c, addr = s.accept()
fin = open(fileName,'rb')
l = fin.read(512)
while (l):
c.send(l)
l = fin.read(512)
fin.close()
s.shutdown(1) #works for 1 and 2 AND without. Maybe doesnt need this...
#s.close()
def client_thread(clientsocket, file_names):
file_name = file_names.pop()
clientsocket.send(file_name.ljust(64)[0:64])
connect = fileConnection()
connect.sendFile("./folder/" + file_name, clientsocket)
##################################################
#puts filenames in fileNames
##################################################
file_names = []
for (dirpath, dirnames, filenames) in os.walk("./folder/"):
file_names.extend(filenames)
break
##################################################
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind((serverIP, 9999))
serversocket.listen(5)
print "Listening for jobs to send...\n"
while True:
(clientsocket, address) = serversocket.accept()
ct = thread.start_new_thread(client_thread,(clientsocket, file_names,))
客户代码:
import easygui
import os.path
import socket
import sys
import os
import time
import errno
from socket import error as socket_error
import tarfile
import shutil
from timeout import timeout
serverIP = "192.168.1.65"
class fileConnection(object):
def connect(self, client_socket):
port = int(client_socket.recv(5).strip())
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connected = False
while not connected:
try:
sock.connect((serverIP,port))
connected = True
except socket_error:
print "Couldn't connect to server. Trying again in 5 seconds"
time.sleep(5)
print port
return sock
def receiveFile(self, fileName, client_socket):
s = self.connect(client_socket)
fout = open(fileName,'wb')
l = s.recv(512)
while (l):
fout.write(l)
l = s.recv(512)
fout.close()
s.close()
#time.sleep(0.25)
return True
def sendFile(self, fileName, client_socket):
s = self.connect(client_socket)
fin = open(fileName,'rb')
l = fin.read(512)
while (l):
s.send(l)
l = fin.read(512)
fin.close()
s.close()
#time.sleep(0.25)
return True
def test_receive_file(client_socket):
file_name = client_socket.recv(64).strip()
print file_name
connect = fileConnection()
connect.receiveFile("./folder/" + file_name, client_socket)
return file_name
while True:
#time.sleep(.1)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((serverIP, 9999))
file_name = test_receive_file(client_socket)
client_socket.close()