Starting the server
The program we just wrote does absolutely nothing, so let's update it so that it at least starts a server, using tokio
. Let's write an actual body to our server()
function:
use std::net::{IpAddr, Ipv4Addr, SocketAddr}; use tokio_core::reactor::Handle; use tokio_core::net::TcpListener; #[async] fn server(handle: Handle) -> io::Result<()> { let port = 1234; let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), port); let listener = TcpListener::bind(&addr, &handle)?; println!("Waiting clients on port {}...", port); #[async] for (stream, addr) in listener.incoming() { let address = format!("[address : {}]", addr); println!("New client: {}", address); handle.spawn(handle_client(stream)); println!("Waiting another client..."); } Ok(()) }
The function now takes a Handle
, which will be useful to specify on which event loop the server must run. We start this function by specifying...