Rust 不接收来自 C++ 的 UDP 消息

UDP messages from C++ are not received by Rust(Rust 不接收来自 C++ 的 UDP 消息)

本文介绍了Rust 不接收来自 C++ 的 UDP 消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UDP 创建服务器/客户端范例,但 Rust 服务器没有接收 C++ 客户端消息.我已经能够成功地进行 Rust 服务器/Rust 客户端和 C++ 服务器/Rust 客户端通信.

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.

这让我相信我的 C++ 代码存在问题,或者在将 C++ 缓冲区发送到 Rust 时存在某种类型的错误通信,但我使用了我认为有效的代码.这只是从同一台计算机发送和发送到同一台计算机,尚未扩展到计算机到计算机.

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.

我不是 UDP/TCP 方面的专家,所以我可能做错了什么

I am no expert with UDP / TCP so I may be doing something incorrectly

Rust 服务器:

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++ 客户端:

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();

C++ 客户端声明数据已发送(sent 变量)并且没有错误(err 变量).但是,我的 Rust 服务器从不接收数据.它被设置为非阻塞,所以它只是坐在那里等待接收数据(它在客户端发送到端口 12000 时查看端口 12000).

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).

推荐答案

当你连接一个 UDP 套接字时,这会导致 UDP 套接字只从它所连接的地址接收数据报.服务器不应连接其 UDP 套接字.

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.

这篇关于Rust 不接收来自 C++ 的 UDP 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Rust 不接收来自 C++ 的 UDP 消息

基础教程推荐