TCP Socket question

Cary O'Brien cobrien at Radix.Net
Tue Nov 14 22:44:08 EST 2000


In article <3A11FF34.57496FFF at home.com>, Rick Lee  <rwklee at home.com> wrote:
>Let me rephrase my question, now that I have played around with socket a
>little bit.
>
>If bufsize is smaller than the size of the message that the sender sends
>me, the rest is waiting for me at the next s.recv(bufsize).  My question
>is: how do I know this condition has occurred?
>

You can use select to find out if there is any more data in the socket.

>I am in a situation where: I don't really know what the maximum message
>size the sender can send to me.  And let's say there is no way in the
>message itself to tell if the end has been reached.
>
>If I just blindly try a second s.recv(bufsize) if the first received
>string length is the same as bufsize, I run the chance that I will be
>blocked, if in fact the sender's message size is exactly bufsize.
>

You need to use select.  If you really don't know how much data is
in the socket, you also want to make it non-blocking so that you
won't hang if you try to read too much.

Also (although this doesn't matter so much in python) that reading
1024 bytes from a socket doesn't necessaraly return 1024 bytes. It
can return form 1 to 1024.

Similarly for writes.  You need to check the return code for the
write to see how much was written.  If your reciever isn't reading
fast enough you can try to write 1024 and actually end up sending
nothing.

Actually, does python re-try writes?  Re-try reads?

The *BEST* thing to do is to agree on a protocol that has the
packet length in the (fixed length) header.  Or you can 
look at the socket-server library stuff.

-- cary

>- Rick Lee
>
>Rick Lee wrote:
>
>> Hi,
>>
>> I have a question about the s.recv method for a TCP socket:  The syntax
>> is:
>>
>>     s.recv (bufsize [,flags])
>>
>> And the manual says: "Receives data from the socket.  The data is
>> returned as a string.  The maximum amount of data to be received is
>> specified by bufsize..."
>>
>> My question is: what happens if the sender's message is larger than
>> bufsize?  Is the rest discarded, or what?  Also, how would I know that
>> this condition has occurred?  Note that the flags values don't seem to
>> help with this question.
>>
>> Thanks in advance
>>
>> - Rick Lee
>























More information about the Python-list mailing list