问题
I'm creating a server / client paradigm using UDP, but the Rust server is not receiving the C++ client messages. I have been able to successfully do Rust server / Rust client and C++ server / Rust client communication.
This leads me to believe that there is an issue with my C++ code, or there is some type of miscommunication when sending C++ buffers to Rust, but I have used code that I beleive works. This is only being sent from and to the same computer and has not been expanded to computer to computer.
I am no expert with UDP / TCP so I may be doing something incorrectly
Rust server:
use std::net::UdpSocket;
fn main() {
let udp: UdpSocket = UdpSocket::bind("0.0.0.0:12000")
.expect("Failed to bind to address for sending/receiving messages");
udp.connect("127.0.0.1:12683")
.expect("Failed to connect address receiving our messages");
//The below (recv_from) is set to blocking
let mut buf = [0; 20];
let (number_of_bytes, src_addr) = udp.recv_from(&mut buf).expect("Didn't receive data");
let filled_buf = &mut buf[..number_of_bytes];
println!("{:?}", filled_buf);
}
C++ client:
boost::asio::io_service io_service;
ip::udp::socket socket( io_service );
ip::udp::endpoint remote_endpoint;
std::cout << "sending reply..." << std::endl;
socket.open( ip::udp::v4() );
remote_endpoint = ip::udp::endpoint( ip::address::from_string( "127.0.0.1" ), 12000 );
unsigned char buff[8]{ 5,5,5,5,5,5,5,5 };
boost::system::error_code err;
//auto sent = socket.send_to( buffer( "Jane Doe"), remote_endpoint, 0, err );
auto sent = socket.send_to( buffer( buff ), remote_endpoint, 0, err );
std::cout << err << std::endl;
std::cout << "Sent: " << sent << std::endl;
socket.close();
The C++ client states that the data was sent (sent
variable) and there is no err (err
variable). However, my Rust server never receives the data. It is set to non-blocking so it just sits there waiting to receive data (its looking at port 12000 while the client is sending to port 12000).
回答1:
When you connect a UDP socket, that causes the UDP socket to only receive datagrams from the address it is connected to. Servers should not connect their UDP sockets.
来源:https://stackoverflow.com/questions/55711985/udp-messages-from-c-are-not-received-by-rust