P2P Chat Problem

Hi! I want my program to be a server and a client at the same time. This is what I've written so far. How can I finish it?

pub fn run()
{
    let sendSocket = std::net::TcpListener::bind("127.0.0.1:4440").expect("");
    println!("Waiting...");

    let (mut socket, addr) = sendSocket.accept().expect("");
    println!("Connected! {}", addr);

    let receive = std::thread::spawn( ||{
        let mut inputBuffer = vec![0; 1024];
        socket.read_exact(&mut inputBuffer).expect("");
        let msg = inputBuffer.into_iter().take_while(|&x| x != 0).collect::<Vec<_>>();
        let msg = String::from_utf8(msg).expect("");
        println!("-{}", msg);
    });
    let send = std::thread::spawn(|| {
        let mut sendMsg = String::new();
        std::io::stdin().read_line(&mut sendMsg).expect("");
        sendMsg = sendMsg.trim().to_string();
        let mut buff = sendMsg.clone().into_bytes();
        buff.resize(1024, 0);
        socket.write_all(&buff).expect("");
    });

The problem is that after I move the socket to the sender I can't receive anything.
Thanks in advance!

If you put it in an Arc, then you can clone the arc to get several handles to the same shared socket, and move a clone into each thread. You should be able to call the write and read methods on the socket even if it's wrapped in an Arc.

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.