赞
踩
①监听连接函数
use std::net::{TcpListener,TcpStream}; //接受tcp内容做处理 fn handle_client(_stream:TcpStream) { println!("123455"); } fn main()->std::io::Result<()> { let listener = TcpListener::bind("127.0.0.1:8080")?; //问号表示如果bind成功则创建listener往下走,失败则返回 //接受链接并处理 for stream in listener.incoming() { handle_client(stream?); } //成功返回空括号的result Ok(()) }
use std::net::{TcpListener,TcpStream}; use std::io::*; //接受tcp内容做处理 fn handle_client(mut stream:TcpStream) { //创建缓冲区来读 let mut buffer=[0;512]; stream.read(&mut buffer).unwrap(); println!("Request:{}",String::from_utf8_lossy(&buffer[..])); } fn main()->std::io::Result<()> { let listener = TcpListener::bind("127.0.0.1:8080")?; //问号表示如果bind成功则创建listener往下走,失败则返回 //接受链接并处理 for stream in listener.incoming() { handle_client(stream?); } //成功返回空括号的result Ok(()) }
//返回响应
let reponse = "HTTP/1.1 200 OK\r\n\r\n";
stream.write(reponse.as_bytes()).unwrap();
stream.flush().unwrap();
2)返回一个真正的网页
①创建一个简单的main.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
<hl>Hello</hl>
<p>This is a response from a Rust Server</p>
</body>
</html>
//返回响应
let content = fs::read_to_string("main.html").unwrap();
//内容加载到响应行里面
let reponse = format!("HTTP/1.1 200 OK\r\n\r\n{}",content);
stream.write(reponse.as_bytes()).unwrap();
stream.flush().unwrap();
use std::net::{TcpListener,TcpStream}; use std::io::{Read,Write}; use std::fs; //接受tcp内容做处理 fn handle_client(mut stream:TcpStream) { //创建缓冲区来读 let mut buffer=[0;512]; stream.read(&mut buffer).unwrap(); //打印请求格式 println!("Request:{}",String::from_utf8_lossy(&buffer[..])); //返回响应 let content = fs::read_to_string("main.html").unwrap(); //内容加载到响应行里面 let reponse = format!("HTTP/1.1 200 OK\r\n\r\n{}",content); stream.write(reponse.as_bytes()).unwrap(); stream.flush().unwrap(); } fn main()->std::io::Result<()> { let listener = TcpListener::bind("127.0.0.1:8080")?; //问号表示如果bind成功则创建listener往下走,失败则返回 //接受链接并处理 for stream in listener.incoming() { handle_client(stream?); } //成功返回空括号的result Ok(()) }
1)404Erorr界面的html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
<hl>Oops</hl>
<p>Sorry,error!</p>
</body>
</html>
2)代码示例
use std::net::{TcpListener,TcpStream}; use std::io::{Read,Write}; use std::fs; //接受tcp内容做处理 fn handle_client(mut stream:TcpStream) { //创建缓冲区来读 let mut buffer=[0;512]; stream.read(&mut buffer).unwrap(); //打印请求格式 println!("Request:{}",String::from_utf8_lossy(&buffer[..])); //根据是不是get返回对应内容 let get = b"Get / HTTP/1.1/r\n"; if buffer.starts_with(get){ //返回main.html //返回响应 let content = fs::read_to_string("main.html").unwrap(); //内容加载到响应行里面 let reponse = format!("HTTP/1.1 200 OK\r\n\r\n{}",content); }else{ //返回响应 let content = fs::read_to_string("404.html").unwrap(); //内容加载到响应行里面 let reponse = format!("HTTP/1.1 404 NOT FOUND\r\n\r\n{}",content); } stream.write(reponse.as_bytes()).unwrap(); stream.flush().unwrap(); } fn main()->std::io::Result<()> { let listener = TcpListener::bind("127.0.0.1:8080")?; //问号表示如果bind成功则创建listener往下走,失败则返回 //接受链接并处理 for stream in listener.incoming() { handle_client(stream?); } //成功返回空括号的result Ok(()) }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。