我在python套接字編程中做了一個簡單的服務器客戶端代碼。客戶端將其側面的截圖(圖像)發送給服務器。它正常工作時,我正在傳輸圖像使用'localhost'即從一個文件夾到其他.....但主要問題出現時,圖像傳輸到另一臺計算機上......服務器端收到的圖像被破壞...進一步更多我觀察到客戶端(發送方圖像,即非損壞的一個)和服務器(接收方圖像,即損壞的一個)之間的區別幾乎每次1Kb .........python套接字編程中的接收端數據丟失
我的客戶端代碼(發送者)側is--
os.system('scrot screen.bmp') #command to take screen shot
FILE = "screen.bmp"
f = open(FILE, "rb")
data = f.read()
f.close()
del f
imagesize = int(os.path.getsize('screen.bmp'))
sendsize = '%1024s' %imagesize
s.sendall(str(sendsize))
print 'length of data = ',len(data)
s.sendall(str(len(data)))
s.sendall(str(data))
和服務器側(接收端)---
filename='screen.bmp'
print '[Media] Starting media transfer for ',filename
os.system('rm -f screen.bmp')
f = open(filename,"wb")
expsizeimage = int(conn.recv(1024))
data1 = conn.recv(1024)
data2=''
for i in range(0,len(data1)):
if(not(data1[i]=='0' or data1[i]=='1' or data1[i]=='2' or data1[i]=='3' or data1[i]=='4' or data1[i]=='5' or data1[i]=='6' or data1[i]=='7' or data1[i]=='8' or data1[i]=='9')):
break
data2=data2+data1[i]
print '------------'+data2+'-------------'+str(m)+'----------------'
print 'size of data:' ,int(data2)
print 'the expected size of image is: ', expsizeimage
data=9
del data
sized=0;
while 1:
data = conn.recv(expsizeimage)
print 'received length of image = ',len(data)
f.write(data)
sized=sized+len(data)
print "sized------"+str(sized)
del data
if(sized>=int(data2)):
break
print "saved the screentshot data recieved"
你明確'break'ing如果你收到的所有數據除了1024字節。你期望會發生什麼? – geoffspear
當沒有數據時,應該可以安全地中斷,比如「if not data:break」。你不需要做任何幻想來消磨時間。 – Cryptite