当前位置:网站首页>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
边栏推荐
- The difference between deep copy and shallow copy
- Transfer learning of five categories of pictures based on VGg
- GDAL + ogr learning
- [UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
- Go的Gin框架学习
- Use of list - addition, deletion, modification and query
- 读取excel,int 数字时间转时间
- Dock installation redis
- Docker 安装 Redis
- word frequency count
猜你喜欢

Eigen learning summary
![Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag](/img/ed/4d61ce34f830209f5adbddf9165676.png)
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
![[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)](/img/ae/cbfc01fbcc816915b1794a9d70247a.png)
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)

Process management command

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

Jenkspy package installation

Fashion classification case based on keras

C#的随机数生成

2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展

Classification of cifar100 data set based on convolutional neural network
随机推荐
Auto.js 自定义对话框
String function in MySQL
Operation of 2022 mobile crane driver national question bank simulation examination platform
C# 网络相关操作
C [file operation] read TXT text by line
Implementation of k8s redis one master multi slave dynamic capacity expansion
Fashion classification case based on keras
MySQL 中的字符串函数
C language implements memcpy, memset, strcpy, strncpy, StrCmp, strncmp and strlen
Remember using Ali Font Icon Library for the first time
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
587. Install fence / Sword finger offer II 014 Anagrams in strings
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
What are the relationships and differences between threads and processes
How to install jsonpath package
Timestamp to formatted date
Map basemap Library
Anchor location - how to set the distance between the anchor and the top of the page. The anchor is located and offset from the top
Encapsulate a timestamp to date method on string prototype
读取excel,int 数字时间转时间