有没有办法检查连接是否已关闭然后完成线程?

时间:2018-02-22 10:23:06

标签: multithreading sockets networking rust tcplistener

使用类似于this example的代码,连接会永远继续,因为无限循环永远不会停止。我修改了一下,以便允许读取不阻塞线程:

use std::net::{TcpListener, TcpStream};
use std::io::{BufRead, BufReader};
use std::thread;

fn main() {
    let listener = TcpListener::bind("127.0.0.1:4444").unwrap();
    println!("Server is running on 127.0.0.1:4444 ...");

    for stream in listener.incoming() {
        let stream = stream.unwrap();
        stream
            .set_nonblocking(true)
            .expect("Failed to set stream as nonblocking");
        thread::spawn(move || handle_client(stream));
    }
}

fn handle_client(stream: TcpStream) {
    let mut stream = BufReader::new(stream);
    println!("New client logged");

    loop {
        let mut recv_buffer = String::new();
        println!("Before reading");
        let recv_data_size = stream
            .read_line(&mut recv_buffer)
            .expect("Error when reading line");

        if recv_data_size > 0 {
            println!("{}", recv_buffer);
        }
    }
}

客户端非常简单,然后连接发送Hello!到服务器:

use std::io::prelude::*;
use std::net::TcpStream;

fn main() {
    let mut stream = TcpStream::connect("127.0.0.1:4444").unwrap();
    let _ = stream.write("Hello!".as_bytes());
}

当客户端发送其字符串并且断开连接时,服务器进入无限循环并无限期地打印Before reading。它永远不知道何时关闭连接,即使客户很久以前就结束了它。

在Rust中是否有办法检查远程连接是否已关闭(如果可能,无需设置超时),然后正确完成运行它的线程?

1 个答案:

答案 0 :(得分:0)

来自read_line的文档:

  

返回一个空缓冲区表示该流已达到EOF。

所以你可以这样做:

fn handle_client(stream: TcpStream) {
    let mut stream = BufReader::new(stream);
    println!("New client logged");

    loop {
        let mut recv_buffer = String::new();
        println!("Before reading");
        let recv_data_size = stream
            .read_line(&mut recv_buffer)
            .expect("Error when reading line");

        if recv_data_size > 0 {
            println!("{}", recv_buffer);
        } else {
            // stream has reached EOF
            break;
        }
    }
}