Question

I was trying to find examples about socket programming and came upon this script: http://stacklessexamples.googlecode.com/svn/trunk/examples/networking/mud.py

When reading through this script i found this line: listenSocket.listen(5)

As i understand it - it reads 5 bytes from the buffer and then does stuff with it...

but what happens if more than 5 bytes were sent by the other end?

in the other place of that script it checks input against 4 commands and sees if there is \r\n in the string. dont commands like "look" plus \r\n make up for more than 5 bytes?

Alan

Was it helpful?

Solution

The following is applicable to sockets in general, but it should help answer your specific question about using sockets from Python.

socket.listen() is used on a server socket to listen for incoming connection requests.

The parameter passed to listen is called the backlog and it means how many connections should the socket accept and put in a pending buffer until you finish your call to accept(). That applies to connections that are waiting to connect to your server socket between the time you have called listen() and the time you have finished a matching call to accept().

So, in your example you're setting the backlog to 5 connections.

Note.. if you set your backlog to 5 connections, the following connections (6th, 7th etc.) will be dropped and the connecting socket will receive an error connecting message (something like a "host actively refused the connection" message)

OTHER TIPS

This might help you understand the code: http://www.amk.ca/python/howto/sockets/

The argument 5 to listenSocket.listen isn't the number of bytes to read or buffer, it's the backlog:

socket.listen(backlog)

Listen for connections made to the socket. The backlog argument specifies the maximum number of queued connections and should be at least 1; the maximum value is system-dependent (usually 5).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top