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



On Jan 16, 7:44 am, Lars Uffmann <a...@xxxxxxxxxxxxxx> wrote:

What's the "natural" approach to "defuse" a function that is listening
for incoming packets?

There are several ways, some of which naturally fit your situation and
some of which don't:

1) Defuse the socket in a safe way that assures that the blocking
function will return. This is not possible for UDP, though it is
possible with TCP, using 'shutdown'.

2) Send the thread a signal to make it abort its operation. I do not
recommend this approach.

3) Set a shutdown flag and then create the event the thread is waiting
for. That is, if the thread is waiting for a UDP packet on port 402,
send it one. Make sure it checks the shutdown flag as soon as its
blocking function finishes and discards the packet.

4) Set the socket non-blocking. Create a pipe. Use 'select' or 'poll'
to wait for both the socket and the pipe. Block in 'select' or 'poll'.
When you want to shutdown the thread, send a byte on the pipe. This
will cause the thread to unblock.

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?

Yes, but that won't work. UDP in unreliable. By the time you call
'recvfrom', the packet could be dropped, and you could miss a
shutdown. (Precisely this type of bug caused an infamous Linux inetd
UDP denial of service attack.)

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.

See the list of approaches above.

DS
.



Relevant Pages

  • Re: UDP sockets bound to ANY send answers with wrong src ip address
    ... >> I observe that UDP sockets listening on ANY ... >> instead of using dst ip address of client's packet. ... I can bind a temporary socket to needed src address, ... for (cmsgptr = CMSG_FIRSTHDR(&msg); ...
    (Linux-Kernel)
  • Re: Strange UDP Socket problem
    ... UDP packet to an IP that doesn't have anything at the other end that my ... > I suspect you would get the same response if you used one thread to send ... > thread on same socket. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Tying UDP socket to stdin, stdout
    ... build a pseudo-telnet based on UDP for obtaining my results. ... Netcat seems to be cool but It won't be useful to me. ... based on the inter packet delays and buffering of packets and I need ... how to connect the UDP socket to stdin and stdout. ...
    (comp.unix.programmer)
  • Re: Socket does not receive UDP packet with source address 255.255.255.255
    ... Most likely the packet is dropped as invalid/corrupt at the ... UDP layer. ... > My problem is that I want the socket (I've tried both with the MFC ... > int nRet; ...
    (microsoft.public.win32.programmer.networks)
  • Re: How to terminate a socket in CLOSE_WAIT state
    ... FTP Server fixed for certain FTP clients who use both passive ... This was causing a PASSIVE opened socket to be left ... but instead expect the "half close" from the receiver. ... this sends a TCP/IP FIN packet to the ...
    (microsoft.public.win32.programmer.kernel)