Sending a file over TCP sockets in Python -


i've been able copy file contents (image) new file. when try same thing on tcp sockets i'm facing issues. server loop not exiting. client loop exits when reaches eof, server unable recognize eof.

here's code:

server

import socket               # import socket module  s = socket.socket()         # create socket object host = socket.gethostname() # local machine name port = 12345                 # reserve port service. s.bind((host, port))        # bind port f = open('torecv.png','wb') s.listen(5)                 # wait client connection. while true:     c, addr = s.accept()     # establish connection client.     print 'got connection from', addr     print "receiving..."     l = c.recv(1024)     while (l):         print "receiving..."         f.write(l)         l = c.recv(1024)     f.close()     print "done receiving"     c.send('thank connecting')     c.close()                # close connection 

client

import socket               # import socket module  s = socket.socket()         # create socket object host = socket.gethostname() # local machine name port = 12345                 # reserve port service.  s.connect((host, port)) s.send("hello server!") f = open('tosend.png','rb') print 'sending...' l = f.read(1024) while (l):     print 'sending...'     s.send(l)     l = f.read(1024) f.close() print "done sending" print s.recv(1024) s.close                     # close socket when done 

here's screenshot:

server server

client client

edit 1: data copied over. making file "not complete." first column shows image has been received. seems larger 1 sent. because of this, i'm not able open image. seems corrupted file.

file sizes

edit 2: how in console. file sizes same here. python console same file sizes

client need notify finished sending, using socket.shutdown (not socket.close close both reading/writing part of socket):

... print "done sending" s.shutdown(socket.shut_wr) print s.recv(1024) s.close() 

update

client sends hello server! server; written file in server side.

s.send("hello server!") 

remove above line avoid it.


Comments

Popular posts from this blog

javascript - Wordpress slider, not displayed 100% width -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -