Skip to content

Commit

Permalink
set_reuse_port
Browse files Browse the repository at this point in the history
  • Loading branch information
vnt-dev committed Aug 6, 2024
1 parent 0f9ac2a commit afff49e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion vnt/src/channel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
//端口可能冲突,则使用任意端口
Expand Down
2 changes: 2 additions & 0 deletions vnt/src/channel/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand Down
2 changes: 1 addition & 1 deletion vnt/src/channel/tcp_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}
Expand Down

0 comments on commit afff49e

Please sign in to comment.