Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: CTRL+C doesn't quit the process properly anymore using a blocking socket  (Read 3683 times)

0 Members and 1 Guest are viewing this topic.

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Hi guys,
I'm using SFMLs network library to send simple data packets to a single client. That part works flawlessly.

When my program starts it initializes and in the end waits for a client using TCPListener.accept() with a blocking socket. When I want to close the program (console application) via CTLR+C "something" happens (e.g. OpenCV closes the connection to the webcam etc.) but the process itself doesn't quit. I have to terminate it via task manager.

If I comment the listening part out the program stops at the end of the main function as planned, so it should be a problem with the blocking socket.

What I tried is setting the socket unblocking and polling every 50ms in a while loop using sf::sleep(). But that also prevents the process to quit completely. I'd be okay using a keypress inside the while to break the loop and let the program run to the end of main, but I don't know how, as there is no window to get any events / key presses and I didn't find a way to do non-blocking console I/O via C++.

Edit:
Additional information:

Windows 8.1
SFML 2.2
Visual Studio 2013
Other used libraries: OpenCV, ArUco (AR marker tracking)

I start the process via the windows console command window.
« Last Edit: April 06, 2015, 12:37:31 am by Daddi »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Which OS?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
Sorry, forgot the important things :D

Windows 8.1
SFML 2.2
Visual Studio 2013
Other used libraries: OpenCV, ArUco (AR marker tracking)

I start the process via the windows console command window.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
I've no idea what the requirements are for the SIGINT (or signals in general), but for me it seems a bit presumptuous to assume that any application will shutdown gracefully when sending the SIGINT signal while not actually handling any signals. ;)

Signals are a basic OS feature and if one wants to use them with a certain purpose, one might as well handle them (see the MSDN).

As for the whole blocking part, you might want to read this thread and binary1248's points.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
I know about signals and already tried using a custom signal handler. It gets fired and prints a message, but even the following hard exit(1); does not stop the process.

The thread you mentioned did I read before I opened the thread, but I don't really get in which way this will help me with my problem? As I stated in the first post, I already tried using a non-blocking architecture.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Guess it's time to get your debugger and find out where your thread hangs. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Daddi

  • Jr. Member
  • **
  • Posts: 66
    • View Profile
    • http://foxdev.de/
    • Email
After some more debugging I found out, that OpenCVs video grabber blocks the process somehow. If I don't initialize the camera, everything works as intended (the TCPListener is not to blame). Thanks for your help :)

If anybody reads my comment from above and looks for a way to do "unblocking I/O" in C++:
You can use the function kbhit() to get a boolean if any character was hit on the keyboard. May come in handy if you need to break an infinite loop without having a window open (no events / keyboard available).

 

anything