当前位置:网站首页>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江西光伏展,中国分布式光伏展会,南昌太阳能利用展
- .105Location
- Vite configure proxy proxy to solve cross domain
- word frequency count
- Welcome to the markdown editor
- Stanford machine learning course summary
- C# 的数据流加密与解密
- 2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
- k8s之实现redis一主多从动态扩缩容
- undefined reference to `Nabo::NearestNeighbourSearch
猜你喜欢

.104History

Implementation of k8s redis one master multi slave dynamic capacity expansion

Clion installation tutorial

Batch export ArcGIS attribute table

C#的随机数生成
![[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)

Random number generation of C #

解决允许在postman中写入注释请求接口方法
Scikit learn sklearn 0.18 official document Chinese version

ArcGIS table to excel exceeds the upper limit, conversion failed
随机推荐
Go的Gin框架学习
C# 网络相关操作
Multi thread crawling Marco Polo network supplier data
Notes on common basic usage of eigen Library
How to read literature
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
Map basemap Library
word frequency count
re正則錶達式
Climbing watermelon video URL
MySQL_01_简单数据检索
Remember using Ali Font Icon Library for the first time
ArcGIS license error -15 solution
C#字节数组(byte[])和字符串相互转换
Crack sliding verification code
Thirteen documents in software engineering
C language loop structure program
消费者灰度实现思路
ArcGIS table to excel exceeds the upper limit, conversion failed
Format problems encountered in word typesetting