set_reuse_port

This commit is contained in:
lbl8603
2024-08-06 22:03:18 +08:00
parent 0f9ac2ac87
commit afff49e61a
3 changed files with 6 additions and 2 deletions
+3 -1
View File
@@ -262,7 +262,9 @@ pub(crate) fn init_context(
let socket = socket2::Socket::new(socket2::Domain::IPV4, socket2::Type::STREAM, None)?;
(socket, address)
};
let _ = socket.set_reuse_address(true);
socket.set_reuse_address(true)?;
#[cfg(unix)]
socket.set_reuse_port(true)?;
if let Err(e) = socket.bind(&address.into()) {
if ports[0] == 0 {
//端口可能冲突,则使用任意端口
+2
View File
@@ -62,6 +62,8 @@ pub fn create_tcp0(
}
if bind_port != 0 {
socket.set_reuse_address(true)?;
#[cfg(unix)]
socket.set_reuse_port(true)?;
if v4 {
let addr: SocketAddr = format!("0.0.0.0:{}", bind_port).parse().unwrap();
socket.bind(&addr.into())?;
+1 -1
View File
@@ -83,7 +83,7 @@ async fn connect_tcp_handle<H>(
};
tokio::spawn(async move {
if let Err(e) = connect_tcp0(data, addr, recv_handler, context, bind_port).await {
log::warn!("发送失败,链接终止:{:?},{:?}", addr, e);
log::warn!("连接失败,链接终止:{:?},{:?}", addr, e);
}
});
}