What is the problem with my attempt to translate code from java to rust?

I'm trying to do a "handshake" with a minecraft server on Rust. I'm building on this answer and this wiki.

The code from the java response works, but any requests sent to the server via UdpSocket on Rust give the error " The remote host forcibly terminated an existing connection. (os error 10054)".

For example: I started minecraft and opened the world to the local network. The server address is 127.0.0.1: 50312 (port may change). Requests from java works, however such code is in Rust (a simple ping attempt):

let mut socket = UdpSocket::bind("127.0.0.1:50001").expect("Failed to connect");

let address = "127.0.0.1";
let port: u16 = 50312;

let addr = format!("{}:{}", address, port);
let mut data = [0u8; 2];
data[0] = 0x01;
socket.send_to(&data, addr.clone());

Returns the above error on subsequent listening to the socket

Here is the complete unfinished code of my queries (the code is experimental)

Question: What am I doing wrong? How to do this correctly?

Author: USSURATONCAHI, 2020-12-17

1 answers

At first glance, you can immediately see that there are different protocols. The Java example uses the Socket class, which uses the TCP / IP protocol under the hood. The Rust example above uses UdpSocket, which uses a different protocol - UDP/IP.

In Java, a UDP / IP socket is called a DatagramSocket.

You just need to change UdpSocket to another one that works over TCP/IP.

 1
Author: Maxgmer, 2020-12-17 18:18:19