Skip to content

how to split UdpSocket? #3755

Answered by Darksonn
ta3pks asked this question in Q&A
Discussion options

You must be logged in to vote

This is explained in the documentation for UdpSocket.

This type does not provide a split method, because this functionality can be achieved by instead wrapping the socket in an Arc. Note that you do not need a Mutex to share the UdpSocket — an Arc<UdpSocket> is enough. This is because all of the methods take &self instead of &mut self. Once you have wrapped it in an Arc, you can call .clone() on the Arc<UdpSocket> to get multiple shared handles to the same socket. An example of such usage can be found further down.

There is also an example in the documentation:

use tokio::{net::UdpSocket, sync::mpsc};
use std::{io, net::SocketAddr, sync::Arc};

#[tokio::main]
async fn main() -> io::Result<

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by taiki-e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants