Trevor Boyd Smith
Trevor Boyd Smith

Reputation: 19273

Is it valid to use SO_LINGER with a udp socket?

Is it valid to use SO_LINGER with a udp socket?

If yes, would you please describe the situation or illustrative-example where SO_LINGER would be appropriate with a udp socket?


a little background:

I have never used the SO_LINGER option so I am unfamiliar with what it does or when it is appropriate to use (especially with a udp socket).

more specific context for this question:

I ask because I ran across some open source code that was implementing a udp socket and using the SO_LINGER socket option (from my 30 seconds of looking at the code it looks like a pretty standard udp socket receive and then pushing the data out to some sort of consumer via gnuradio's API).

From the short reading I have done on SO_LINGER on man pages and webpages all the pages have talked about SO_LINGER being used with connection oriented protocols (i.e. tcp, et al.)... but this code is doing a non-connection oriented protocol, udp in this specific case, so I am confused why the SO_LINGER is being used with a udp socket.

Upvotes: 1

Views: 2058

Answers (1)

user207421
user207421

Reputation: 310980

When used with a positive read timeout, it causes close() to block for up to that timeout while the socket send buffer still has unsent data in it, and any difficulty writing that data will be returned as an error from the close() method. This applies equally well to UDP as to TCP. It is little used.

The other use of SO_LINGER, with a zero timeout, applies only to TCP.

Upvotes: 3

Related Questions