The error gets out:

error: no method named `write` found for type `std::io::BufWriter<&&mut std::net::TcpStream>` in the current scope --> <anon>:89:28 | 89 | writer.write(b"OK"); | ^^^^^ | = note: the method `write` exists but the following trait bounds were not satisfied: `&&mut std::net::TcpStream : std::io::Write` 

I can not figure out how to send the message "OK" to the client after receiving his data packet.

Playground URL and Git URL

 use std::net::{TcpListener, TcpStream}; use std::io::{BufReader, BufWriter}; use std::io::prelude::*; use std::thread; //use commands; pub struct LoginServer { address: String, // reader: BufReader<TcpStream>, } impl LoginServer { pub fn new(hostname: &str, port: &str) -> LoginServer { let address = format!("{}:{}", hostname, port); // let stream = TcpStream::connect(&*address).unwrap(); LoginServer { address: address, // reader: BufReader::new(stream), } } pub fn start(&mut self) -> bool { let listener = match TcpListener::bind(&*self.address) { Ok(data) => data, Err(e) => { println!("Ошибка открытия порта: {}", e); return false; }, }; for stream in listener.incoming() { match stream { Ok(mut stream) => { let address = self.address.clone(); thread::spawn(move || { handle_client(address, &mut stream); }); }, Err(e) => { println!("Ошибка при запуске сервера: {}", e); return false; } } } fn handle_client(address: String, client_stream: &mut TcpStream) { /*println!("Подключен неизвестный клиент, ip: {}:{}", reader.get_ref().local_addr().unwrap().ip(), reader.get_ref().local_addr().unwrap().port());*/ let mut writer = BufWriter::new(&client_stream); loop { let mut data = String::new(); let result = { let mut reader = BufReader::new(client_stream); match reader.read_line(&mut data).unwrap() { 0 => { println!("Неизвестный клиент был отключен, ip: {}:{}", reader.get_ref().local_addr().unwrap().ip(), reader.get_ref().local_addr().unwrap().port()); return; }, _ => (), } let mut server_stream = TcpStream::connect(&*address).unwrap(); println!("Принял данные: {}", data); let data = data.trim(); let data: Vec<&str> = data.split_whitespace().collect(); match data[0] { "login" => true, //commands::login(reader.get_mut(), &mut server_stream, &data[1..]), "register" => true, //commands::new_account(&data[1..]), _ => false, } }; if !result { println!("Неверная команда"); } else { writer.write(b"OK"); //let client_reference = client_stream.by_ref(); //let _ = client_reference.write(b"OK"); } } } true } } 
  • "I can't figure it out" is still not a very good style for a question on StackOverflow. Give specific errors and your attempts to eliminate them (even if they are unsuccessful). - Michael Pankov
  • `error [E0277]: the trait bound &&mut std::net::TcpStream: std::io::Write is not satisfied -> <anon>: 53: 30 | 53 | let mut writer = BufWriter :: new (& client_stream); | ^^^^^^^^^^^^^^^^^ | ` - Alex Centurius
  • for the future - this should be in the question itself - Michael Pankov
  • OK. (thirteen characters) - Alex Centurius

1 answer 1

Problem solved, now data is being received. When creating, BufReader replaced client_stream with its pointer &client_stream . Also with BufWriter .

And when sending a response to the client instead of writer.write(b"OK") , you need to write

 writer.write(b"OK").unwrap(); writer.flush().unwrap(); 

The entire working code: https://gist.github.com/b59174f800109ef0d1620bada24d4505

  • It is also worth noting this answer as “adopted” with the help of a tick under the vote. - Michael Pankov
  • mark in 2 days. - Alex Sotnik