473,320 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Stopping a socket.accept() method

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

--
please send any mail to botedesschattens(at)web(dot)de
Jul 18 '05 #1
3 14284
Tobias Pfeiffer wrote:
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
Jul 18 '05 #2
Hi!

On 16 Aug 2004, Peter Hansen wrote:
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

--
please send any mail to botedesschattens(at)web(dot)de
Jul 18 '05 #3
Tobias Pfeiffer wrote:
On 16 Aug 2004, Peter Hansen wrote:
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()?


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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the...
2
by: Christopher J. Bottaro | last post by:
Hello, I'm trying to write a fairly simple network program. The main thread spawns a thread which creates a listener socket and then calls socket.accept on it. socket.accept blocks indefinantly....
2
by: Bruce Vander Werf | last post by:
How can I cleanly stop a thread that is currently blocking on Socket.Receive? I don't want to use Thread.Abort, because I would like the thread method to exit cleanly, and the same code must run...
10
by: groups.20.thebriguy | last post by:
socket objects have a little quirk. If you try to receive 0 bytes on a blocking socket, they block. That is, if I call recv(0), it blocks (until some data arrives). I think that's wrong, but I...
4
by: Engineerik | last post by:
I am trying to create a socket server which will listen for connections from multiple clients and call subroutines in a Fortran DLL and pass the results back to the client. The asynchronous socket...
3
by: =?Utf-8?B?UGFpbiBhbmQgaGVhZGFjaGU=?= | last post by:
Hi, I am trying to get a webpage using a TcpSocket instead of a standard Webrequest. Initial, it works fine but after the 2 or 3 request the tcpclient I start to get the following error: A...
3
by: Khookie | last post by:
Hi everyone I've been coding a web server recently, and wanted to figure out how to do it with mingw (code is below). It works fine when you first load a page (GET request), but when you...
4
by: Zytan | last post by:
This may be the dumbest question of all time, but... When I set the packet size, does it mean ALL packets are that size, no matter what? Let's say the packet size is 8KB, and I send a 5 byte...
0
by: Riccardo Di Meo | last post by:
Hi everyone, I'm practicing with embedding python into C code and i have encountered a very strange problem: I'm unable to call the "accept" method of a (correctly created) server socket without...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.