-
Notifications
You must be signed in to change notification settings - Fork 334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No simple example for concurrent connections #322
Comments
Note, just to give an example of what I'm referring to, something like this:
Though ideally w/o the use of the futures crate explicitly, if possible. Instead of something more complex, like this: https://github.com/tokio-rs/tokio/blob/master/examples/manual-runtime.rs |
Here's an approach that works for me. I added lots of println's to illustrate...
output (on my machine I'm running a server on 1935 but not on 34254:
|
If the intention is to run both connections in parallel, you'd probably want to #[tokio::main]
async fn main() {
pretty_env_logger::init();
let addr1:SocketAddr = "127.0.0.1:1935".parse().unwrap();
let addr2:SocketAddr = "127.0.0.1:34254".parse().unwrap();
let one = tokio::spawn(send(addr1, "Hello!".to_string()));
let two = tokio::spawn(send(addr2, "GET".to_string()));
let tasks = vec![one, two];
futures::future::join_all(tasks).await;
println!("done!");
} also, i think this example would be a bit neater (and wouldn't require allocating a #[tokio::main]
async fn main() {
pretty_env_logger::init();
let addr1:SocketAddr = "127.0.0.1:1935".parse().unwrap();
let addr2:SocketAddr = "127.0.0.1:34254".parse().unwrap();
let one = tokio::spawn(send(addr1, "Hello!".to_string()));
let two = tokio::spawn(send(addr2, "GET".to_string()));
tokio::join!(one, two);
println!("done!");
} |
great examples @hawkw ! I had forgotten about the It would be great around this kind of an example to talk about threads vs tasks. I believe Some ideas
|
chore: Fix readme link to docs
Currently there are no simple examples that show an easy "client side" task such as concurrently "sending" two tcp messages.
Essentially all the trivial examples show all the asynchronous tasks being crammed inside the
for_each
function of aTcpListener
.Would you consider adding some examples of the "correct" way of doing a simple tasks such as launching 2 concurrent connections over tcp and waiting until both of them send & receive a message ?
The text was updated successfully, but these errors were encountered: