Closed UDP socket still holds port

For TCP socket when we use close() function and right after that create and bind new TCP socket on same port, we could get "Address already in use" error. Looks like I faced with this error for UDP socket. So my question: is it possible to have "Address already in use" error after closing UDP socket and binding new socket on same port?

Answered by DTS Engineer in 130889022

is it possible to have "Address already in use" error after closing UDP socket and binding new socket on same port?

Yes. You should check out the

SO_REUSEADDR
and
SO_REUSEPORT
socket options.

IMPORTANT This issue isn’t specific to Apple platforms, it’s common to all BSD Sockets implementation, so it’s not covered in depth in Apple docs. If you want a detailed explanation of the issue, I recommend you consult a standard BSD Sockets text. By go-to reference for this is UNIX Network Programming by Stevens et al.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

is it possible to have "Address already in use" error after closing UDP socket and binding new socket on same port?

Yes. You should check out the

SO_REUSEADDR
and
SO_REUSEPORT
socket options.

IMPORTANT This issue isn’t specific to Apple platforms, it’s common to all BSD Sockets implementation, so it’s not covered in depth in Apple docs. If you want a detailed explanation of the issue, I recommend you consult a standard BSD Sockets text. By go-to reference for this is UNIX Network Programming by Stevens et al.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Closed UDP socket still holds port
 
 
Q