当前位置:网站首页>Rust: a simple example of TCP server and client
Rust: a simple example of TCP server and client
2022-04-23 18:04:00 【Xu Yeping】
Rust: Tcp A simple example of server and client
Don't talk much nonsense , Go straight to the chestnuts :
1 Server-side code
use std ::net::{
TcpListener,TcpStream};
use std::thread;
use std::time;
use std::io;
use std::io::{
Read,Write};
fn handle_client(mut stream: TcpStream) -> io::Result<()>{
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").unwrap();
let mut thread_vec: Vec<thread::JoinHandle<()>> = Vec::new();
for stream in listener.incoming() {
let stream = stream.expect("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(())
}
2 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 i in 0..3 {
println!("i = {}", i);
let mut input = String::new();
println!("read_line");
io::stdin().read_line(&mut input).expect("Failed to read");
println!("write");
stream.write(input.as_bytes()).expect("failed to write");
println!("BufReader::new())");
let mut reader =BufReader::new(&stream);
let mut buffer: Vec<u8> = Vec::new();
println!("read_until");
reader.read_until(b'\n',&mut buffer)?;
println!("read form server:{}",str::from_utf8(&buffer).unwrap());
println!("");
}
Ok(())
}
版权声明
本文为[Xu Yeping]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230544498257.html
边栏推荐
- 2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
- JS get link? The following parameter name or value, according to the URL? Judge the parameters after
- Docker 安装 Redis
- An example of linear regression based on tensorflow
- 2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
- .105Location
- 2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
- C language implements memcpy, memset, strcpy, strncpy, StrCmp, strncmp and strlen
- Selenium + webdriver + chrome realize Baidu to search for pictures
- C medium? This form of
猜你喜欢

Auto. JS custom dialog box

Gobang game based on pyGame Library
Logic regression principle and code implementation

C network related operations

Implementation of k8s redis one master multi slave dynamic capacity expansion

From source code to executable file

Qt读写XML文件(含源码+注释)

Nat commun | current progress and open challenges of applied deep learning in Bioscience

.104History

Map basemap Library
随机推荐
Installation du docker redis
读取excel,int 数字时间转时间
Docker 安裝 Redis
YOLOv4剪枝【附代码】
From source code to executable file
Submit local warehouse and synchronize code cloud warehouse
Fashion classification case based on keras
Thirteen documents in software engineering
Summary of floating point double precision, single precision and half precision knowledge
xlsxwriter. exceptions. Filecreateerror: [errno 13] permission denied
Re expression régulière
Process management command
.105Location
.104History
Svn simple operation command
Operators in C language
Amount input box, used for recharge and withdrawal
Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
word frequency count
ROS package NMEA_ navsat_ Driver reads GPS and Beidou Positioning Information Notes