This commit is contained in:
Christien Rioux 2024-03-26 10:22:41 -05:00
parent 309492e9a8
commit 8fd0491d11

View File

@ -81,14 +81,21 @@ pub fn new_bound_first_udp_socket(local_address: SocketAddr) -> io::Result<Socke
}
}
// Bind the socket -first- without turning on SO_REUSEPORT this way it will
// fail if the port is already taken
cfg_if! {
if #[cfg(unix)] {
socket
.set_reuse_address(true)?;
}
}
socket.bind(&socket2_addr)?;
// Set 'reuse address' so future binds to this port will succeed
// This does not work on Windows, where reuse options can not be set after the bind
cfg_if! {
if #[cfg(unix)] {
socket
.set_reuse_address(true)?;
socket.set_reuse_port(true)?;
}
}
@ -165,17 +172,23 @@ pub fn new_bound_first_tcp_socket(local_address: SocketAddr) -> io::Result<Socke
}
}
// Bind the socket -first- before turning on 'reuse address' this way it will
// Bind the socket -first- without turning on SO_REUSEPORT this way it will
// fail if the port is already taken
let socket2_addr = SockAddr::from(local_address);
cfg_if! {
if #[cfg(unix)] {
socket
.set_reuse_address(true)?;
}
}
socket.bind(&socket2_addr)?;
// Set 'reuse address' so future binds to this port will succeed
// This does not work on Windows, where reuse options can not be set after the bind
cfg_if! {
if #[cfg(unix)] {
socket
.set_reuse_address(true)?;
socket.set_reuse_port(true)?;
}
}