set_reuse_port
This commit is contained in:
@@ -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 {
|
||||
//端口可能冲突,则使用任意端口
|
||||
|
||||
@@ -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())?;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user