2025-07-11 23:03:21 +08:00
|
|
|
use tokio::io::AsyncWriteExt;
|
|
|
|
use tokio::net::{TcpListener, TcpStream};
|
2025-07-10 18:27:09 +08:00
|
|
|
|
|
|
|
pub(crate) struct TcpServer;
|
|
|
|
|
|
|
|
impl TcpServer {
|
|
|
|
pub(crate) async fn init() {
|
|
|
|
match TcpListener::bind("127.0.0.1:12345").await {
|
|
|
|
Ok(listener) => loop {
|
|
|
|
match listener.accept().await {
|
|
|
|
Ok((socket, addr)) => {
|
|
|
|
log::info!("New client connected: {addr}");
|
|
|
|
|
|
|
|
tokio::spawn(async { Self::process(socket).await });
|
|
|
|
}
|
|
|
|
Err(e) => log::error!("Couldn't get client: {e:?}"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => log::error!("Failed to bind port: {e:?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn process(mut socket: TcpStream) {
|
|
|
|
socket
|
|
|
|
.write_all("Hello from server written in Rust!".as_bytes())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|