Socket Programming: How to terminate a thread "listening" for UDP packets?



Hi everyone!

I have started out with socket programming by developing a little tool that will read a file transfer from telemetry (in this case being streamed via UDP). The first version is very rudimentary, meaning I do not control correct sequence of packets and missing packets, I just dump everything to a file. And - hooray - it's working ;)

What I am trying to do is - I want to allow the user in a basic GUI to click on a button and toggle the program status between "listening" and "not listening" for new file transfers / packets. Right now, whenever my thread is activated that listens for incoming UDP packets, I open a socket and bind it to my local ip address, then do a call to recvfrom (...) - but that waits until a packet arrives. Meaning I don't get to check the variable that would tell my thread to shut itself down gracefully. And I don't see a way to ungracefully shut it down from the button event procedure (i.e. kill the thread), but that's probably a problem specific to the library I'm using (widestudio).

What's the "natural" approach to "defuse" a function that is listening for incoming packets? Is there maybe a function in the socket library that will tell me IF there is data to be read (or not) instantly, which I can call and only start a recvfrom() call when there is actually data to be read?

I'm not so much looking for a specific solution to my problem, more the general approach to implementing such stuff - I'm still a beginner ;) don't wanna succumb to the dark side of the force.

Any help appreciated!

Lars
.