gpt4 book ai didi

SslStream 读取不返回客户端的消息

转载 作者:行者123 更新时间:2023-12-03 11:31:13 24 4
gpt4 key购买 nike

我正在尝试使用 TLS (openssl) 实现客户端-服务器应用程序。我按照 rust doc 中给出的示例作为我的代码结构:example
服务器代码

fn handle_client(mut stream: SslStream<TcpStream>){
println!("Passed in handling method");
let mut data = vec![];
let length = stream.read(&mut data).unwrap();
println!("read successfully; size read:{}", length);

stream.write(b"From server").unwrap();
stream.flush().unwrap();

println!("{}", String::from_utf8_lossy(&data));
}


fn main() {
//remember: certificate should always be signed

let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls()).unwrap();
acceptor.set_private_key_file("src/keyfile/key.pem", SslFiletype::PEM).unwrap();
acceptor.set_certificate_file("src/keyfile/certs.pem",SslFiletype::PEM).unwrap();
acceptor.check_private_key().unwrap();
let acceptor = Arc::new(acceptor.build());

let listener = TcpListener::bind("127.0.0.1:9000").unwrap();

for stream in listener.incoming(){
match stream{
Ok(stream)=>{
println!("a receiver is connected");
let acceptor = acceptor.clone();
//thread::spawn(move || {
let stream = acceptor.accept(stream).unwrap();
handle_client(stream);
//});
}
Err(_e)=>{println!{"connection failed"}}
}
}


println!("Server");
}
客户代码
fn main() {
let mut connector = SslConnector::builder(SslMethod::tls()).unwrap();
connector.set_verify(SslVerifyMode::NONE); //Deactivated verification due to authentication error
connector.set_ca_file("src/keyfile/certs.pem");
let connector = connector.build();

let stream = TcpStream::connect("127.0.0.1:9000").unwrap();
let mut stream = connector.connect("127.0.0.1",stream).unwrap();

stream.write(b"From Client").unwrap();
stream.flush().unwrap();
println!("client sent its message");

let mut res = vec![];
stream.read_to_end(&mut res).unwrap();
println!("{}", String::from_utf8_lossy(&res));
// stream.write_all(b"client").unwrap();

println!("Client");
}
服务器代码和客户端代码都编译没有问题,尽管有一些警告。客户端能够连接到服务器。但是当客户端将其消息 From Client 写入流时, stream.read 在 handle_client() 中调用什么都不返回。此外,当服务器从服务器写入其消息时,客户端能够接收到该消息。
因此,我使用 SslStream 的方式或配置服务器的方式是否存在问题 ?

最佳答案

我猜你说 stream.read什么都不返回,它返回一个零值,表示没有读取任何内容。
Read 特征 API 是这样说的:

This function does not provide any guarantees about whether it blockswaiting for data, but if an object needs to block for a read andcannot, it will typically signal this via an Err return value.

If n is 0, then it can indicate one of two scenarios:

  • This reader has reached its "end of file" and will likely no longer beable to produce bytes. Note that this does not mean that the readerwill always no longer be able to produce bytes.

  • The buffer specified was 0 bytes in length.

It is not an error if the returned value n is smaller than the buffersize, even when the reader is not at the end of the stream yet. Thismay happen for example because fewer bytes are actually availableright now (e. g. being close to end-of-file) or because read() wasinterrupted by a signal.


所以,你需要反复调用 read直到你收到你期望的所有字节,或者你得到一个错误。
如果你确切地知道你想读多少(就像你在这种情况下所做的那样),你可以调用 read_exact它将准确读取填充提供的缓冲区所需的字节数。
如果你想读到一个分隔符(例如换行符或其他字符),你可以使用 BufReader ,它提供了 read_until 等方法或 read_line .

关于SslStream<TcpStream> 读取不返回客户端的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65878583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com