2012-05-15 45 views
0

我在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" 
+4

你明確'break'ing如果你收到的所有數據除了1024字節。你期望會發生什麼? – geoffspear

+0

當沒有數據時,應該可以安全地中斷,比如「if not data:break」。你不需要做任何幻想來消磨時間。 – Cryptite

回答

0

這兩個程序未經測試,是爲最新版本的Python編寫的,並且出於安全目的切換您的客戶端/服務器關係。


Source.py

import os, struct, socket 

def main(): 
    # Take screenshot and load the data. 
    os.system('scrot image.bmp') 
    with open('image.bmp', 'rb') as file: 
     data = file.read() 
    # Construct message with data size. 
    size = struct.pack('!I', len(data)) 
    message = size + data 
    # Open up a server socket. 
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    server.bind(('', 65000)) 
    server.listen(5) 
    # Constantly server incoming clients. 
    while True: 
     client, address = server.accept() 
     print('Sending data to:', address) 
     # Send the data and shutdown properly. 
     client.sendall(message) 
     client.shutdown(socket.SHUT_RDWR) 
     client.close() 

if __name__ == '__main__': 
    main() 

Destination.py

import socket, struct 

def main(host): 
    # Connect to server and get image size. 
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    client.connect((host, 65000)) 
    packed = recvall(client, struct.calcsize('!I')) 
    # Decode the size and get the image data. 
    size = struct.unpack('!I', packed)[0] 
    print('Receiving data from:', host) 
    data = recvall(client, size) 
    # Shutdown the socket and create the image file. 
    client.shutdown(socket.SHUT_RDWR) 
    client.close() 
    with open('image.bmp', 'wb') as file: 
     file.write(data) 

def recvall(sock, size): 
    message = bytearray() 
    # Loop until all expected data is received. 
    while len(message) < size: 
     buffer = sock.recv(size - len(message)) 
     if not buffer: 
      # End of stream was found when unexpected. 
      raise EOFError('Could not receive all expected data!') 
     message.extend(buffer) 
    return bytes(message) 

if __name__ == '__main__': 
    main('localhost') 
+0

它給了一個錯誤回溯(最近通話最後一個): 文件 「stackc.py」,30日線在 主( 'localhost' 的) 文件 「stackc.py」,6號線,在主 client.connect ((host,50001)) 文件「/usr/lib/python2.7/socket.py」,行224,在meth return getattr(self._sock,name)(* args) socket.error:[Errno 111]拒絕連接 –

+0

您需要在運行客戶端(Destination.py)之前運行服務器(Source.py)。另外,您可能需要考慮它在此程序中使用的'bytes'類型,並使用'str'(取決於您使用的Python版本)。 –

+0

仍然遇到同樣的問題 –

相關問題