I am creating a UDP tunnel client program to receive random packets from client OS system and send the data packets to a UDP server using a reader thread function. Similarly I am reading packets from the UDP server and sending it to the client's tunnel IP address. The reader and writer threads must run simultaneously. I get the following error when I execute the program.
use of moved value: `socket`
value used here after moverustcE0382
basic.rs(68, 38): value moved into closure here
basic.rs(74, 21): variable moved due to use in closure
basic.rs(101, 21): use occurs due to use in closure
basic.rs(58, 9): move occurs because `socket` has type `UdpSocket`, which does not implement the `Copy` trait
How do I declare the socket variable as global variable to remove the error occurring at writer thread when I use the keyword move?
let socket = UdpSocket::bind("8.0.0.1:8000") //create client socket object with ip and port
.expect("Could not bind client socket");
socket.connect("8.0.0.10:8888") //SERVER IP /PORT
.expect("Could not connect to server");
thread::sleep(time::Duration::from_secs(5)); // PROVIDING TIME TO ESTABLISH SOCKET CONNECTION TO SERVER
let reader_session = session.clone();
let reader = std::thread::spawn( move || {
while RUNNING.load(Ordering::Relaxed) {
match reader_session.receive_blocking() {
Ok(packet) => { //receiving random packets from OS
let bytes = packet.bytes();
let leng = bytes.len();
socket.send(&bytes[0..leng]); //SENDING RANDOM PACKETS TO SERVER
println!
(
"Version & IHL: {} Service Type: {} Packet Length: {} {} Identification: {} {} Flags&Fragment: {} {}
TTL: {} Protocol: {} Checksum: {} {} SourceIP: {}.{}.{}.{} DestIP: {}.{}.{}.{}",
&bytes[0],&bytes[1],&bytes[2],&bytes[3],&bytes[4],&bytes[5],&bytes[6],&bytes[7],
&bytes[8],&bytes[9],&bytes[10],&bytes[11],
&bytes[12],&bytes[13],&bytes[14],&bytes[15],&bytes[16],&bytes[17],&bytes[18],&bytes[19]
);
println!();
}
Err(_) => println!("Got error while reading packet"),
}
}
});
let writer_session = session.clone();
let writer_socket = socket.try_clone()?;
let writer = std::thread::spawn(move || {
info!("Starting writer");
while RUNNING.load(Ordering::Relaxed) {
let mut buffer = [0u8; 1500];
writer_socket.recv_from(&mut buffer) //get message from server
.expect("Could not read into buffer");
let mut leng1 = buffer.len();
println!("From server: {}", str::from_utf8(&buffer) //print message from server
.expect("Could not write buffer as string"));
socket.send_to(&buffer[0..leng1],"6.0.0.1"); //send message from server to tunnel ip
}
});
CodePudding user response:
You should be able to clone the UdpSocket
using the try_clone()
method which creates a new variable referencing the same underlying socket.
e.g.
...
let reader_session = session.clone();
let reader_socket = socket.try_clone().expect("Couldn't clone the socket");
let reader = std::thread::spawn( move || {
...
reader_socket.send(&bytes[0..leng]);
https://doc.rust-lang.org/std/net/struct.UdpSocket.html#method.try_clone