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
});
}
}
Client:
#[tokio::main]
async fn main() {
let mut stream: TcpStream = TcpStream::connect("127.0.0.1:8080").await.unwrap();
println!("Hello, world!");
}
Any help or pointers would be much appreciated!
Marko