如何实现一个像telnet命令一样读写的客户端?
How to implement a client that writes and reads like the telenet command?
我想做一个 echo 服务器的基本 rust 和 tokio 实现,代码基于 Creating a Chat Server with async Rust and Tokio,服务器工作完美,但我的客户端有问题,因为当客户端是一个程序时只是写入它工作正常,但是当你希望它像 telnet 命令(写入和读取)一样工作时它不起作用,它挂起读取并且消息甚至没有到达
use std::env::args;
use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
net::{TcpListener, TcpStream},
};
#[tokio::main]
async fn main() {
let mut args = args();
args.next();
match args.next().as_deref() {
Some("master") => {
let listener = TcpListener::bind("localhost:8080").await.unwrap();
loop {
let (mut socket, addr) = listener.accept().await.unwrap();
println!("new connection from {:#?}", addr);
tokio::spawn(async move {
let (reader, mut writer) = socket.split();
let mut reader = BufReader::new(reader);
let mut line = String::new();
loop {
let bytes_read = reader.read_line(&mut line).await.unwrap();
if bytes_read == 0 {
break;
}
println!("{}", &line.trim());
writer.write_all(line.as_bytes()).await.unwrap();
line.clear();
}
});
}
}
Some("client") => {
let mut stream = TcpStream::connect("localhost:8080").await.unwrap();
let (reader, mut writer) = stream.split();
writer.write_all(b"Hello!").await.unwrap();
let mut reader = BufReader::new(reader);
let mut line = String::new();
let bytes_read = reader.read_line(&mut line).await.unwrap(); // HANGS HERE
println!("{}", &line);
line.clear();
}
_ => {}
}
}
连接到服务器的 telnet 命令的输出:
# telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello! // hello sent by user
hello! // hello sent by the server
服务器没有发送换行符,但客户端正在等待。它适用于 telnet,因为 telnet 会在字符到达时打印字符,而无需等待整行。
我想做一个 echo 服务器的基本 rust 和 tokio 实现,代码基于 Creating a Chat Server with async Rust and Tokio,服务器工作完美,但我的客户端有问题,因为当客户端是一个程序时只是写入它工作正常,但是当你希望它像 telnet 命令(写入和读取)一样工作时它不起作用,它挂起读取并且消息甚至没有到达
use std::env::args;
use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
net::{TcpListener, TcpStream},
};
#[tokio::main]
async fn main() {
let mut args = args();
args.next();
match args.next().as_deref() {
Some("master") => {
let listener = TcpListener::bind("localhost:8080").await.unwrap();
loop {
let (mut socket, addr) = listener.accept().await.unwrap();
println!("new connection from {:#?}", addr);
tokio::spawn(async move {
let (reader, mut writer) = socket.split();
let mut reader = BufReader::new(reader);
let mut line = String::new();
loop {
let bytes_read = reader.read_line(&mut line).await.unwrap();
if bytes_read == 0 {
break;
}
println!("{}", &line.trim());
writer.write_all(line.as_bytes()).await.unwrap();
line.clear();
}
});
}
}
Some("client") => {
let mut stream = TcpStream::connect("localhost:8080").await.unwrap();
let (reader, mut writer) = stream.split();
writer.write_all(b"Hello!").await.unwrap();
let mut reader = BufReader::new(reader);
let mut line = String::new();
let bytes_read = reader.read_line(&mut line).await.unwrap(); // HANGS HERE
println!("{}", &line);
line.clear();
}
_ => {}
}
}
连接到服务器的 telnet 命令的输出:
# telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
hello! // hello sent by user
hello! // hello sent by the server
服务器没有发送换行符,但客户端正在等待。它适用于 telnet,因为 telnet 会在字符到达时打印字符,而无需等待整行。