为 UDP 客户端程序创建全局变量
Creating global variables for UDP client program
我正在创建一个 UDP 隧道客户端程序,以从客户端 OS 系统接收随机数据包,并使用 reader 线程函数将数据包发送到 UDP 服务器。同样,我从 UDP 服务器读取数据包并将其发送到客户端的隧道 IP 地址。 reader 和 writer 线程必须同时 运行。执行程序时出现以下错误。
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
如何将socket变量声明为全局变量,以消除使用关键字move时writer线程出现的错误?
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
}
});
您应该能够使用 try_clone()
方法克隆 UdpSocket
,该方法创建一个引用相同底层套接字的新变量。
例如
...
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
我正在创建一个 UDP 隧道客户端程序,以从客户端 OS 系统接收随机数据包,并使用 reader 线程函数将数据包发送到 UDP 服务器。同样,我从 UDP 服务器读取数据包并将其发送到客户端的隧道 IP 地址。 reader 和 writer 线程必须同时 运行。执行程序时出现以下错误。
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
如何将socket变量声明为全局变量,以消除使用关键字move时writer线程出现的错误?
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
}
});
您应该能够使用 try_clone()
方法克隆 UdpSocket
,该方法创建一个引用相同底层套接字的新变量。
例如
...
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