标准库的 std::net
提供了相关实现。TcpListener
TcpStream
tcp server
use std::io::{self, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::thread;
use std::time;
fn handle_client(mut stream: TcpStream) -> io::Result<()> {
// stream buffer
let mut buf = [0; 512];
for _ in 0..1000 {
let bytes_read = stream.read(&mut buf)?;
if bytes_read == 0 {
return Ok(());
}
stream.write(&buf[..bytes_read])?;
thread::sleep(time::Duration::from_secs(1));
}
Ok(())
}
fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080")?;
let mut thread_vec: Vec<thread::JoinHandle<()>> = Vec::new();
for stream in listener.incoming() {
let stream = stream.expect("stream failed");
let handle = thread::spawn(move || {
handle_client(stream).unwrap_or_else(|error| eprintln!("{}", error))
});
thread_vec.push(handle);
}
for handle in thread_vec {
handle.join().unwrap();
}
Ok(())
}
client
use std::io::{self, prelude::*, BufReader, Write};
use std::net::TcpStream;
use std::str;
fn main() -> io::Result<()> {
let mut stream = TcpStream::connect("127.0.0.1:8080")?;
for _ in 0..10 {
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read");
stream.write(input.as_bytes()).expect("Failed to write");
let mut reader = BufReader::new(&stream);
let mut buffer: Vec<u8> = Vec::new();
reader
.read_until(b'\n', &mut buffer)
.expect("Failed to read buffer");
println!("read from server: {}", str::from_utf8(&buffer).unwrap());
}
println!("THE END");
Ok(())
}