Close a socket externally

drs drs at remove-to-send-mail-ecpsoftware.com
Fri Oct 1 20:26:09 EDT 2004


<bryanjugglercryptographer at yahoo.com> wrote in message
news:1096673569.579558.114260 at k26g2000oda.googlegroups.com...
>
> drs wrote:
>
> > I have a program which opens a socket server in a thread.  I need for
> the
> > server to listen on the socket for a certain amount of time (say, ten
> > seconds or so) and then close it, and am wondering if there is a good
> way to
> > close the socket other than sending it a message from another thread
> to
> > close itself.
>
> Yes, you can either set a timeout on the socket, or use
> select.select() with a timeout.  When waiting for new
> connections, a socket will select as readable when a new
> connection arrives.
>
> > That is, I am looking for a way to forcably kill a socket
> > from a second thread, not to send it a message to kill itself.
>
> The timeout/select solution is along different lines, but if I
> understood your initial statement of the problem, it's how most
> sockets programmers do that kind of thing.

My understanding of using a timeout is that the clock starts running only
when there is no communication.  I actually need a hard limit on the time a
socket is open, and while the best option might be to at least allow the
current read/write cycle to complete, I am not necessarilly adverse to just
cutting off the socket.

To explain, I am working on an agent based modeling project where agents
communicate and exchange over TCP sockets.  Agents have to "buy" the right
to open sockets, so I need an enforcement mechanism to shut them down when
their time expires.

I am not familiar w/ select, so i'll go look into that.

-drs





More information about the Python-list mailing list