title: Can I maintain a Vec of TcpStreams to write to them across threads whenever one of them reads a new input?
body: <p>I want to do message broadcasting: when one of the clients sends a message, the server writes it to each socket. My main problem is I can't figure out how to send the <code>Vec</code> to the threads. I can't use <code>Mutex</code> because that will lock the access of other threads to the <code>Vec</code> for reading. I can't clone and send because <code>TcpStream</code> can't  be cloned and sent. Here's my attempt until now</p>
  
  <pre><code>use std::net::{TcpStream, TcpListener};
  use std::io::prelude::*;
  use std::sync::{Arc, Mutex};
  use std::thread;
  use std::sync::mpsc::{channel, Receiver};
  use std::cell::RefCell;
  
  type StreamSet = Arc&lt;RefCell&lt;Vec&lt;TcpStream&gt;&gt;&gt;;
  type StreamReceiver = Arc&lt;Mutex&lt;Receiver&lt;StreamSet&gt;&gt;&gt;;
  
  fn main() {
      let listener = TcpListener::bind("0.0.0.0:8000").unwrap();
      let mut connection_set: StreamSet = Arc::new(RefCell::new(vec![]));
      let mut id = 0;
      let (tx, rx) = channel();
      let rx = Arc::new(Mutex::new(rx));
      for stream in listener.incoming() {
          let receiver = rx.clone();
          let stream = stream.unwrap();
          (*connection_set).borrow_mut().push(stream);
          println!("A connection established with client {}", id);
          thread::spawn(move || handle_connection(receiver, id));
          id += 1;
          tx.send(connection_set.clone()).unwrap();
      }
  
  }
  
  fn handle_connection(rx: StreamReceiver, id: usize) {
      let streams;
      {
          streams = *(rx.lock().unwrap().recv().unwrap()).borrow();
      }
      let mut connection = &amp;streams[id];
      loop {
          let mut buffer = [0; 512];
          if let Err(_) = connection.read(&amp;mut buffer) {
              break;
          };
          println!("Request: {}", String::from_utf8_lossy(&amp;buffer[..]));
          if let Err(_) = connection.write(&amp;buffer[..]) {
              break;
          };
          if let Err(_) = connection.flush() {
              break;
          };
      }
  }
  </code></pre>