当前位置:网站首页>Rust web (I) -- self built TCP server
Rust web (I) -- self built TCP server
2022-07-27 02:54:00 【Shi Shang がりへ】
I learned a little some time ago Rust Basic content of , Out of learning Web Development requirements , Also to consolidate what I learned Rust Basics , Just try to record your study Rust Web A little bit of ;
Implement environment
OS: Ubuntu 14.0
IDE: PyCharm
This article is a personal study note , About Rust The basic part of does not mention too much , It is inevitable that there will be omissions in some details , Please also consult carefully ;
Catalog
Rust Web( One )—— build by oneself TCP Server
Four 、 Message sending and receiving
5、 ... and 、 The overall structure of the project
Rust Web( One )—— build by oneself TCP Server
One 、 Project creation
Use at your choice
cargoCreate the initial project , It's calledpro1;
Enter project directory , Create two more sub projects , Respectively
tcpserver、tcpclient;
open
pro1MediumCargo.tomlfile , Add subprojects to the current workspace , As follows :
![]()
Two 、TCP Server
Enter subproject
tcpserver, opensrcNext Ofmian.rsfile ;The following is the specific design process :
Library Introduction
introduce Rust Self contained network programming library
std::net::TcpListener

Listener binding
Create one with specific
IP:portBound listening instance ;

Message reception
For message reception , There are two options :
One is Only accept once The operation ends immediately after the message ( It's not commonly used );
The other is except for artificial interruption Infinite loop receive , This kind of scheme is more used in actual design ;
The specific implementation reference is as follows
One time reception

Cyclic reception

In right formula
streamBelong tostd::result::Result<>type , So you need to useunwrap()Simple exception handling ;
use std::net::TcpListener;
fn main() {
let listener = TcpListener::bind("127.0.0.1:2333").unwrap();
println!("Running on port 2333...");
for stream in listener.incoming() {
let _stream = stream.unwrap();
println!("Connection established!");
}
}Preliminary operation
Here's how it works ;
3、 ... and 、TCP Client
Enter subproject
tcpclient, go tosrc/main.rs;The specific steps are as follows :
Library Introduction
introduce Rust Bring your own library
std::net::TcpStream;Create a data flow instance to connect to the server at the specified address ;
use std::net::TcpStream;
fn main() {
let stream = TcpStream::connect("127.0.0.1:2333").unwrap();
}Running effect
Client

Server

Four 、 Message sending and receiving
Because the network programming library introduced realizes Read and write Trait, So we need to introduce two Trait
std::io::{Read, Write}TCP Reading and writing in message transmission Original byte string form ;
Server
use std::net::TcpListener;
use std::io::{Read, Write};
fn main() {
let listener = TcpListener::bind("127.0.0.1:2333").unwrap();
println!("Running on port 2333...");
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();
}
}Client
Additional warehousing is required at the client
std::str
use std::net::TcpStream;
use std::io::{Read, Write};
use std::str;
fn main() {
let mut stream = TcpStream::connect("127.0.0.1:2333").unwrap();
stream.write("Here's CLient End".as_bytes()).unwrap();
let mut buffer = [0; 17]; // Here 17 Is to match the length of the message
stream.read(&mut buffer).unwrap();
println!("Response from server: {:?}",
str::from_utf8(&mut buffer).unwrap() // utf-8 Receive in encoded form
);
}Running effect
Server

Client

5、 ... and 、 The overall structure of the project

Every day I don't dance , It's all a betrayal of life .
边栏推荐
- 蚂蚁京东新浪10位架构师424页佳作深入分布式缓存从原理到实践pdf
- It has been established for 3 years, and now goose factory has an annual income of millions +. As some suggestions of software testing predecessors
- Static routing experiment configuration
- Web3.0世界知识体系分享-什么是Web3.0
- 信息收集-端口扫描工具-Nmap使用说明
- 用swiper分类图标
- js utils 零碎
- Basic theoretical knowledge of software testing - concept
- 数据资产管理的概念
- LeetCode刷题——NO.238——除自身以外数组的乘积
猜你喜欢

Web3.0 world knowledge system sharing - what is Web3.0

从单表到分表实现数据平滑迁移

Hcip day 3 Wan topology experiment

Redis五种基本数据结构

Why do people like to rank things

人们为什么热衷于给事物排序

I was fired at the age of 30. I want to understand a few things

Smooth data migration from single table to sub table

Dynamically set the height of applet swiper

time模块: 时间戳、结构化时间、格式化时间的获取与相互转化
随机推荐
Hcip day 4 OSPF routing protocol
转:俞敏洪:阻碍你成长的,是你自己
小玩一个并行多线程MCU—MC3172
I wish you a happy Chinese Valentine's day and invite you to read the source code together
Which securities firm is safer to open an account and buy REITs funds?
Leetcode skimming -- no.238 -- product of arrays other than itself
【无标题】
Jmeter接口测试, 快速完成一个单接口请求
bp 插件临时代码记录
Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换
[enchanting interpretation, 15 minutes let you thoroughly learn how to use the stack!!!]
膜拜,阿里内部都在强推的321页互联网创业核心技术pdf,真的跪了
Dynamically set the height of applet swiper
关于序列化变量保存的坑,加了索引器的数据不能序列化
static关键字
Static routing experiment configuration
Interview shock 68: why does TCP need three handshakes?
Rust Web(一)—— 自建TCP Server
【Code】剑指offer 04二维数组中的查找
Basic theoretical knowledge of software testing - concept


