How to close a non-blocking socket?
if we call close system call on a
non-blocking socket it returns
immediately
The socket is always closed: the connection may still be writing to the peer. But your question embodies a fallacy: if you call close() on any socket it will return immediately. Closing and writing to a socket is asynchronous. You can control that with SO_LINGER as per the other answer, although I suspect that only applies to blocking mode. Probably you should put the socket back into blocking mode before closing with a positive SO_LINGER if that's what you need to do.
Closing a UDP Socket when using Non-Blocking IO
Is there any issue associated with closing the file descriptor as soon
as the call to sendto() has completed given that it's non-blocking?
No problems there -- once sendto()
has returned (with a non-error return value), your data has been copied into a system buffer and can be considered "sent". Closing the socket will not prevent the data from going out.
I'm also interested in any thread-safety considerations with doing the
above in multiple threads concurrently?
No problems there either -- since there is no data-sharing across threads, there are no race conditions possible.
Related Topics
Let Non-Root User Write to Linux Host in Docker
Errors in Make File:*** Missing Separator. Stop
How to Detect a Buffer Over Run on Serial Port in Linux Using C++
Parse CSV in Bash and Assign Variables
How to Speed Up Compilation Time in Linux
List Files Recursively in Linux Cli With Path Relative to the Current Directory
A Way to Determine a Process'S "Real" Memory Usage, I.E. Private Dirty Rss
Fast Way to Find Difference Between Two Strings of Equal Length in Perl
Finding an Ip Address from an Interface Name
How to Use Git Namespace to Hide Branches
I Cannot Get a Result from a Single Line Put into the Erlang Shell
What Do the Characters in the Bash Environment Variable $- Mean
Linux' Hrtimer - Microsecond Precision
Linux Write Sys Call Using String on Stack
Shell Variable Issue When Trying to Mkdir
Unable to Start Rstudio in Centos Getting Error "Unable to Connect to Service"