473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 KeyboardInterru pt:
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 botedesschatten s(at)web(dot)de
Jul 18 '05 #1
3 14631
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 KeyboardInterru pt:
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 botedesschatten s(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
9290
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 python wrapper module for sockets, and found that the close command doesn't actually call the underlying close! this didn't seem right, so i added it, and my code now works simply and as expected. def close(self):
2
2382
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. My problem is that when the main thread determines that it is time to quit, how do I get the spawned thread to exit? Preferably I'd like the spawned thread to return from socket.accept when the main thread tells it to and then check a shared...
2
12681
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 under the Compact Framework, which does not support Abort. Will Socket.Close cause the Receive method to finish, or is there a better way?
10
5622
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 don't want to argue that. I would like to create a subclass of socket that fixes the problem. Ideally, something like: class new_socket(socket): def recv( self, bufsize, flags=0 ):
4
3605
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 client and asynchronous socket server example code provided in the .NET framework developers guide is a great start but I have not dealt with sockets before and I am struggling with something. From what I can tell the sample server code ...
3
3090
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 connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
3
25648
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 submit (POST), it usually comes back with a "connection has been reset" error. Sometimes it does work though. Furthermore - when I inspect the request string, it's always like:
4
7071
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 "hello", will it cause 8KB of bandwidth, or 5 bytes (plus TCP/IP packet header, as well, of course). (Btw, I 'set' the packet size via Socket.BeginReceive(), in the "size" field = "The number of bytes to receive." Which seems to imply the answer...
0
1324
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 receiving a "Segmentation fault" (inside the PyObject_CallMethod). My code <seemsto be correct (at least it's correct enough for me to call .getsockname(), .fileno() and other methods without problems), I'm pretty new to this thing though,...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.