tcpserver
use std::{
io::{Read, Write},
net::TcpListener,
};
fn main() {
let listener = TcpListener::bind("127.0.0.1:3000").unwrap();
println!("Running on port 3000...");
for stream in listener.incoming() {
let mut stream = stream.unwrap();
println!("Connection established");
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();
stream.write(&mut buffer).unwrap();
}
}
tcpclient
use std::{
io::{Read, Write},
net::TcpStream,
str,
};
fn main() {
let mut stream = TcpStream::connect("127.0.0.1:3000").unwrap();
stream.write("Hello".as_bytes()).unwrap();
let mut buffer = [0; 5];
stream.read(&mut buffer).unwrap();
println!(
"Response from server:{:?}",
str::from_utf8(&buffer).unwrap()
);
}