Module server
Expand description
TCP server module for Rama.
The TCP server is used to create a TcpListener
and accept incoming connections.
§Example
use rama_tcp::server::TcpListener;
use rama_core::service::service_fn;
use tokio::{io::AsyncWriteExt, net::TcpStream};
const SRC: &str = include_str!("../../../examples/tcp_listener_hello.rs");
#[tokio::main]
async fn main() {
TcpListener::bind("127.0.0.1:9000")
.await
.expect("bind TCP Listener")
.serve(service_fn(|mut stream: TcpStream| async move {
let resp = [
"HTTP/1.1 200 OK",
"Content-Type: text/plain",
format!("Content-Length: {}", SRC.len()).as_str(),
"",
SRC,
"",
]
.join("\r\n");
stream
.write_all(resp.as_bytes())
.await
.expect("write to stream");
Ok::<_, std::convert::Infallible>(())
}))
.await;
}
Structs§
- A TCP socket server, listening for incoming connections once served using one of the
serve
methods such asTcpListener::serve
. - Builder for
TcpListener
.