Code translate error

I have a class called connection in java.

//the constructor and the function I need to implement
Connection(String host, int port) {"Connecting to " + host + ":" + port);
        if (host != null) {
            try {
                this.socket = new Socket(host, port);
       = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                this.out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            } catch (IOException e) {
                throw new ConnectionException("Couldn't connect to Minecraft, is it running?");
    static class ConnectionException extends RuntimeException {

        ConnectionException(String message) {

        ConnectionException(Throwable cause) {
String receive() {
        try {
            return in.readLine();
        } catch (IOException e) {
            throw new ConnectionException(e);

I implement it in rust

//still constructor and function
    pub fn new<A : ToSocketAddrs>(address : A) -> Connection {
        Connection {
            socket : TcpStream::connect(address).expect("Couldn't connect to Minecraft, is it running?"),
            auto_flush: true
    pub fn receive(self) -> String {
        self.clone().socket.set_nonblocking(false).expect("Failed to set non-blocking");
        self.clone().socket.set_read_timeout(Some(Duration::from_secs(1))).expect("Failed to set read timeout");
        let mut s = String::new();
        self.clone().socket.read_to_string(&mut s).expect("Failed to read from socket");
        self.clone().socket.set_nonblocking(true).expect("Failed to set non-blocking");

I do some test and found that the Rust version is always timeout but java runs successfully.
What's the problem in my function?

I think std::io::BufRead::read_line is what you want to use here. But to use it, you first need to wrap the socket in std::io::BufReader, a type implementing std::io::BufRead.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.