Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » How to break the server loop ?
How to break the server loop ? [message #44654] Wed, 06 May 2015 14:25 Go to next message
akebee is currently offline  akebee
Messages: 90
Registered: August 2011
Location: China
Member
for(;;) {
TcpSocket s;
if(s.Accept(server)) {
}

In the server loop, the s.Accept will blocked the thread, So how to exit thread when program-quit ?
Re: How to break the server loop ? [message #44655 is a reply to message #44654] Wed, 06 May 2015 20:15 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

Hi akabee

akebee wrote on Wed, 06 May 2015 14:25
for(;;) {
TcpSocket s;
if(s.Accept(server)) {
}

In the server loop, the s.Accept will blocked the thread, So how to exit thread when program-quit ?


On POSIX systems, the easiest way is probably using signals. You can terminate your program by calling kill(pid, signal). Everything your server is doing, including Accept will be interupted. If you need to perform clean-up tasks etc. on shutdown, you can install signal-handler function, that will be called. To register the handler, signal(sig, function) is used.

You can look at Skylark package, how it is done there. The kill function is called here, The signal handler is installed just a few lines below.

Alternatively, you can use non-blocking Accept(), if I am not mistaken, it should be enough to just call TcpSocket::Timeout(0). I never used it personally, so you should probably check the documentation first, there are some additional hints Wink

Best regards,
Honza
Re: How to break the server loop ? [message #44657 is a reply to message #44655] Thu, 07 May 2015 08:05 Go to previous messageGo to next message
akebee is currently offline  akebee
Messages: 90
Registered: August 2011
Location: China
Member
Hi dolik.rce Rolling Eyes
Im not in POSIX , its Windows.
Now i send a request to my server socket when program-quit, if recieved the specific request the server thread will break and exit.

Is this a good solution ?

Re: How to break the server loop ? [message #44658 is a reply to message #44657] Thu, 07 May 2015 08:36 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate Contributor

akebee wrote on Thu, 07 May 2015 08:05
Hi dolik.rce Rolling Eyes
Im not in POSIX , its Windows.
Now i send a request to my server socket when program-quit, if recieved the specific request the server thread will break and exit.

Is this a good solution ?

It's 'possible' solution Smile Depends on your actual needs. The biggest problem with this solution is, that anyone can remotelly shut down your server, so it is definitelly good for public projects (unless there is reliable authetication & authorization). But for small projects it might be ok, I've used it in past too Smile

If you wan't something better I'd suggest you to try reading about the timeouts in TcpSocket. If you set timeout to reasonably small value (or 0, to make it nonblocking, as I suggest previously), shutting the server down should take just slightly longer than the timeout value. This should work well on any platform, including windows.

Honza
Re: How to break the server loop ? [message #44663 is a reply to message #44658] Mon, 11 May 2015 09:21 Go to previous messageGo to next message
akebee is currently offline  akebee
Messages: 90
Registered: August 2011
Location: China
Member
TimeOut(0) will causes a high CPU usage because the Infinite loops

Twisted Evil

Re: How to break the server loop ? [message #44665 is a reply to message #44663] Mon, 11 May 2015 17:26 Go to previous messageGo to next message
ManfredHerr is currently offline  ManfredHerr
Messages: 67
Registered: February 2013
Location: Germany
Member
If there is nothing to do you can yield the CPU by sleep(1).
Re: How to break the server loop ? [message #44670 is a reply to message #44654] Wed, 13 May 2015 05:10 Go to previous messageGo to next message
akebee is currently offline  akebee
Messages: 90
Registered: August 2011
Location: China
Member
nice Very Happy
Re: How to break the server loop ? [message #46185 is a reply to message #44654] Wed, 23 March 2016 22:58 Go to previous message
nlneilson is currently offline  nlneilson
Messages: 644
Registered: January 2010
Location: U.S. California. Mojave &...
Contributor
For any thread the best way to close it is from within the thread that I have found after many hours of research. That is using C++ or Java
Previous Topic: Mt.cpp broken compile using 9604 nightly snap-shot
Next Topic: HttpRequest [FEATURE]: Add byte range request-header field (patch included)
Goto Forum:
  


Current Time: Tue Apr 16 22:23:33 CEST 2024

Total time taken to generate the page: 0.02066 seconds