Dear Community,
I'm new to Rust but wanted to implement a simple Client-Server using Tokio. I've managed to get it working when using the std::net::{TcpStream,TcpListener} but now I wanted to take up a notch and use Tokio's Tcp. The problem I am having is that I cannot connect my client to the socket where the server is listening.
Server:
async fn process(socket: TcpStream) {
println!("New client: {:?}", socket.peer_addr().unwrap());
}
#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
loop {
let (socket, _) = listener.accept().await?;
tokio::spawn(async move {
// Process each socket concurrently.
process(socket).await
});
}
}
Okey I think I figured out how to resolve the issue, but the technical reasons remain yet to be determined.
If I remove the return type and then replace .await? with .await.unwrap() it works.
Here is the changed version:
#[tokio::main]
async fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();
loop {
let (socket, _) = listener.accept().await?;
tokio::spawn(async move {
// Process each socket concurrently.
process(socket).await
});
}
}
Hey! Thanks for the answer and I should had specified that the print statement does not print for me ... I will try and do the sleep at the end of the client!