Skip to content
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

Multiple servers example #211

Closed
mhristache opened this issue Nov 24, 2020 · 1 comment
Closed

Multiple servers example #211

mhristache opened this issue Nov 24, 2020 · 1 comment

Comments

@mhristache
Copy link

mhristache commented Nov 24, 2020

Hi

I have an application that listens on multiple ports. Currently I am implementing it using threads but I consider changing it to async. Can you provide an example on how I would implement multiple servers (listen on multiple ports and handle connections on each of those ports) using smol runtime?

Thank you

//Max

@ghost
Copy link

ghost commented Nov 25, 2020

You could do something like this:

use smol::prelude::*;

let l1 = TcpListener::bind("localhost:8000").await?;
let l2 = TcpListener::bind("localhost:8001").await?;
let l3 = TcpListener::bind("localhost:8002").await?;

loop {
    let (stream, addr) = l1.race(l2).race(l3).await?;
    println!("accepted client: {:?}", addr);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant