socket + file i/o question

John weekender_ny at yahoo.com
Sun Aug 7 18:08:46 CEST 2005


I am sending a file on a tcp socket using the following code


			while 1:
				buf = os.read(fd, 4096)
				if not buf: break
				print total, len(buf)
				conn.send(buf)

The recieving code looks like

		while 1:
			if recvbytes == filesize:
				print 'Transfer done. Size = %d' % recvbytes
				break
			buf = s.recv(4096)
			if not buf:
				print 'EOF received'
				raise Exception()
			print recvbytes, len(buf)
			os.write(fd, buf)
			recvbytes = recvbytes + len(buf)


My problem is that the first time the client uploads a file
to the server, the code works. But then when the server wants
to download a file to the client, the same code breaks down!

The problem seems to be the socket. The send part sends exactly the
amount of data it reads from the file. But the recv part gets one
more byte than the size of the file?? It seems to me that this
extra one byte is coming inside the send/recv calls. Anyone has
any comments on where this extra one byte is coming from?

Thanks a lot for your help,
--j




More information about the Python-list mailing list