On 6 Nov 2005 09:13:03 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>I wrote two simple socket program.
>one for sending a file and the other for receiving the file.
>but when I run it, a curious thing happened.
>The received file was samller that the sent file.
Your sender does not take care to ensure the entire file is sent. It will
semi-randomly drop bytes from various areas in the middle of the file. Here's
a sender that works correctly:
from twisted.internet import reactor, protocol
from twisted.protocols import basic
from twisted.python import log
filename = sys.argv[1]
host = sys.argv[2]
class Putter(protocol.Protocol):
def connectionMade(self):
fs = basic.FileSender()
d = fs.beginFileTransfer(file(filename, 'b'), self.transport)
d.addCallback(self.finishedTransfer)
d.addErrback(self.transferFailed)
def finishedTransfer(self, result):
self.transport.loseConnection()
def transferFailed(self, err):
print 'Transfer failed'
err.printTraceback()
self.transport.loseConnection()
def connectionLost(self, reason):
reactor.stop()
f = protocol.ClientFactory()
f.protocol = Putter
reactor.connectTCP(host, 9000, f)
reactor.run()
Of course, this is still not entirely correct, since the protocol specified by
your code provides not mechanism for the receiving side to determine whether
the connection was dropped because the file was fully transferred or because of
some transient network problem or other error. One solution to this is to
prefix the file's contents with its length.
Jean-Paul
--
http://mail.python.org/mailman/listinfo/python-list