Frozen socket.

Ray Loyzaga ray at commsecure.com.au
Wed Jun 21 18:17:35 EDT 2000


"C. Porter Bassett" wrote:
> 
> OK, I have been able to duplicate the problem that I was having, but I
> still don't know what to do about it.  First things first, I suppose.
> 
> I made two small programs, a server and a client, and I got the client to
> "freeze" in the exact same way.  My test server accepts three messages,
> but after that, it freezes.  For some reason that I don't understand, the
> client keeps sending messages (about 1000 of them, just like before), even
> though the server is not receiving any.  After about 1000 messages, it
> freezes, just like the program I am debugging.
> 
> Why is the client able to keep sending messages if the server is not
> receiving them?
> 
> In short, what is really happening, and how do I safeguard against it?
> 
> Here is the source for the two test programs I am talking about.
> sample client:
> from socket import *
> import time
> sock = socket(AF_INET, SOCK_STREAM)
> sock.connect("127.0.0.1",2778)
> for i in range(5000):
>    sock.send("This is a string of approximately the correct
> length.          "
>    time.sleep(0.001)
>    print i
> 
> sample server:
> from socket import *
> s = socket(AF_INET, SOCK_STREAM)
> s.bind(""<2778)
> s.listen(5)
> conn, addr = s.accept()
> counter = 0
> while 1:
>    counter = counter + 1
>    data = conn.recv(1024)
>    if couter > 3:
>       while 1:
>          pass
>    if data:
>       print data

Your server sample has a typo "couter", looping on "pass" is just
going to use the CPU, is this really what your real server is doing
when it stops processing input? The s.bind(""<2778)
syntax is another typo, you should have cut and pasted the real
client/server test progs.

With TCP/IP the client is able to stream data ahead of the server's
ability to process data, that is what you are seeing.
In this case the client continues to send until the socket
message buffers are exhausted, at which point it is put to
sleep waiting for more buffers to become available, presumably
because the server has processed some. If you want to limit this
to a smaller amount of data, see getsockopt and play with your
output low water mark. Then use select to ensure that you
are able to write prior to issuing the send operation.



More information about the Python-list mailing list