title: Rust TCP socket server only working with one connection
body: <p>I'm new to Rust and I'm trying to configure a simple tcp socket server which will listen to connections and will reply with the same message that received.</p>
  
  <p>The thing is, this works as I want except when connecting with multiple clients.. The first client that connects will send and receive the messages but if a second client connects, the first one keeps working but the second never receives messages, in fact the message never gets in the code that will handle it. And if I disconnect the first socket, the server will start spamming forever that received a message from the first socket with the same content than the last message it sent.</p>
  
  <p>I am pretty sure I did something wrong in my code but I can't find it</p>
  
  <p>This is my server struct:</p>
  
  <pre><code>use std::collections::HashMap;
  use std::io::Read;
  use std::io::Write;
  use std::net::Shutdown;
  use std::net::TcpListener;
  use std::net::TcpStream;
  use std::str;
  use std::sync::{Arc, RwLock};
  use threadpool::ThreadPool;
  
  #[derive(Clone, Debug)]
  pub struct Server {
      id: Arc&lt;RwLock&lt;u32&gt;&gt;,
      connections: Arc&lt;RwLock&lt;HashMap&lt;u32, TcpStream&gt;&gt;&gt;,
      url: String,
      thread_pool: ThreadPool
  }
  
  impl Server {
      pub fn new(url: String) -&gt; Server {
          let server = Server {
              id: Arc::new(RwLock::new(0)),
              connections: Arc::new(RwLock::new(HashMap::new())),
              url,
              thread_pool: ThreadPool::new(10)
          };
  
          server
      }
  
      pub fn start(&amp;self) {
          let listener = TcpListener::bind(&amp;self.url).expect("Could not start the server");
  
          println!("Server started succesfully");
  
          for stream in listener.incoming() {
              match stream {
                  Ok(stream) =&gt; {
                      let mut self_clone = self.clone();
  
                      self.thread_pool.execute(move || {
                          self_clone.on_client_connect(stream.try_clone().unwrap());
                      });
                  }
                  Err(error) =&gt; eprintln!("Error when tried to use stream. Error = {:?}", error),
              }
          }
      }
  
      fn on_client_connect(&amp;mut self, stream: TcpStream) {
          println!("Client connected from {}", stream.local_addr().unwrap());
  
          let mut id = self.id.write().unwrap();
          {
              *id += 1;
          }
  
          self.connections
              .write()
              .unwrap()
              .insert(*id, stream.try_clone().unwrap());
  
          let mut stream = stream.try_clone().unwrap();
  
          let mut buffer = [0; 1024];
  
          while match stream.read(&amp;mut buffer) {
              Ok(size) =&gt; {
                  println!(
                      "Message received from {} - {}",
                      id,
                      str::from_utf8(&amp;buffer).unwrap()
                  );
  
                  stream.write_all(&amp;buffer[0..size]).unwrap();
  
                  true
              }
              Err(error) =&gt; {
                  println!(
                      "Error when reading message from socket. Error = {:?}",
                      error
                  );
                  stream.shutdown(Shutdown::Both).unwrap();
  
                  false
              }
          } { }
      }
  }
  
  </code></pre>
  
  <p>And in my main.rs I'm just calling the connect function and the server starts working</p>