Stopping a socket.accept() method

T

Tobias Pfeiffer

Hi!

I am writing a network game where the server runs in a console window.
The person who has opened the server shall be able to stop the process of
accepting new clients. By now, I am handling that like this:

while 1:
try:
bla = socket.accept()
except KeyboardInterrupt:
break
socket.shutdown(2)

So the user can Ctrl+C and no more clients will be accepted.
Unfortunately, this doesn't work in Windows. So how can I give the user
the ability to quit the loop interactively? Any ideas?

Bye
Tobias
 
P

Peter Hansen

Tobias said:
I am writing a network game where the server runs in a console window.
The person who has opened the server shall be able to stop the process of
accepting new clients. By now, I am handling that like this:

while 1:
try:
bla = socket.accept()
except KeyboardInterrupt:
break
socket.shutdown(2)

So the user can Ctrl+C and no more clients will be accepted.
Unfortunately, this doesn't work in Windows. So how can I give the user
the ability to quit the loop interactively? Any ideas?

Several options.

1. Ctrl-Break will work if it's a console program and Ctrl-C
doesn't.

2. You can use a non-blocking socket and select(), which
will allow a periodic wakeup to check a flag that is set
by whatever mechanism you want to tell the prog to stop.

3. Have another thread, which can detect the request to
stop, open a connection to the server thread's socket
so that it will wake up... then it can check a flag
and terminate as in 2.

-Peter
 
T

Tobias Pfeiffer

Hi!

Tobias Pfeiffer wrote:

2. You can use a non-blocking socket and select(), which
will allow a periodic wakeup to check a flag that is set
by whatever mechanism you want to tell the prog to stop.

And how exactly do you propose to code this "non-blocking socket"? I've
no idea how to realize that? Could you maybe give me a little example of
how to work with socket.accept() and select()?

Bye
Tobias
 
P

Peter Hansen

Tobias said:
And how exactly do you propose to code this "non-blocking socket"? I've
no idea how to realize that? Could you maybe give me a little example of
how to work with socket.accept() and select()?

Sure, but did you look for yourself? I'm pretty sure Google
would turn up some good stuff for you if you tried with
"Python non-blocking socket accept select" or some subset
of that. If you look but can't find I'd be happy to help
point you even more specifically in the right direction.
(I suspect checking the documentation for the "select" module
would help too.)

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top