sending a file through sockets

Donn Cave donn at u.washington.edu
Fri Jul 5 12:48:05 EDT 2002


Quoth Bryan Olson <fakeaddress at nowhere.org>:
| brueckd at tbye.com wrote:
...
|> but to send the length of the file first so the receiving end
|> knows if and when it got the whole thing.
|
| That's not really needed.  Just  call shutdown(1) when finished writing.
| The receiver can detect the end when recv() returns zero bytes without
| error, which only happens on clean shutdown.  The socket will also
| select() as readable just before the zero-byte recv().

Fine if the file is all you're sending, but it's like "who needs
an on-off switch on this appliance, when you can just cut the
power cord with a pair of insulated wire cutters."  A byte count
prefix usually is needed, and in the rare case when it isn't,
it doesn't cost much to do it anyway.

| The only snag is if a local error causes the sender to close the socket
| before sending all the data.  Note that in the code, you don't actually
| check that you've received as much data as expected.  The code uses the
| size to know when to stop reading, and the "if not data: break" will do
| that anyway.

Yes, the actual application would certainly want to verify that it
received the complete file.

|> Anyway, here's some sample code, but if
|> you're not already familiar with sockets then you should spend a little
|> time experimenting with them.
|
| Did you test this?  The Python library uses the empty string for
| INADDR_ANY, which is fine for bind(), but I don't think it's legal for
| connect().

So, did you test it?  I did!  According to my results, he's right,
you're wrong.

| I understand it's a basic demo, but I'll note that any real application
| requires some time-out discipline to avoid very long hangs.  Also on
| most Unix systems we'd need to watch out for signals that force system
| calls to return.

We would?  I never have, and I don't notice the standard library
modules doing that.  It might something to think about, but hardly
the given it would be in C.

Does any real application need a timeout discipline?  Some applications
probably need one, but "real" or not doesn't sound like a very useful
way to think about it.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list