Rust不会接收来自C ++的UDP消息 [英] UDP messages from C++ are not received by Rust

查看:83
本文介绍了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

锈服务器:

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( 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消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆