Udp socket error


#1

Hi all, I want use udp socket, but example in rust.lang.org is not work.
I do that, but have error in variable err , and I don’t understand what I doing wrong…
This is my code:

use std::net::UdpSocket;
use std::net::
fn in_i32()->i32{
    let mut stdin = std::io::stdin();
    let input:&mut String = &mut String::new();
    input.clear();
    stdin.read_line(input);
    let x = input.trim().to_string().parse::<i32>().unwrap();
    return x;
}
fn main() {
    let port = 12345;
    let mut socket = UdpSocket::bind("192.168.1.225:2525");
if socket.is_err() {
    let err = socket.is_err();
    println!("failed to read from stdin : {}", err);
    let y = in_i32();
    return;
}
let mut buf = [0,3];
for i in 0..buf.len(){
    buf[i]=i;
    println!("{}",buf[i]);
}
//socket.send_
socket.unwrap().send_to(&[7], ("127.0.0.1", 23451)).unwrap();
let y = in_i32();
}

#2

I think you wanted this:

use std::net::UdpSocket;

fn ask_number() -> String {
    let mut stdin = std::io::stdin();
    let mut input = String::new();
    stdin.read_line(&mut input).unwrap();
    input.trim().to_string()
}

fn main() {
    let socket = UdpSocket::bind("127.0.0.1:23451").ok().expect("fail bind");

    let x = ask_number();
    let buf = x.into_bytes();
    
    socket.send_to(&buf, "127.0.0.1:23451").ok().expect("fail send");
    
    let mut buf = [0; 8];
    let _ = socket.recv_from(&mut buf).ok().expect("fail recv");
    
    println!("{:?}", buf);
}

If you’ve some questions, please ask.


#3

crash then I write this

let socket = UdpSocket::bind("192.168.1.225:2525").ok().expect("fail bind");

#4

Which rustc’s version are you using?

And, could you point the compiler’s error message?


#5

I use 1.0.0 , OS win7 32

Thread '<main>' panicked at 'fail bind' , C:/bot/slave/stable-dist-rustc-win-32/build/src/libcore\option.rs:330

#6

Is this protocol valid?


#7

yes


#8

That should work fine to bind, and it does seem to on my computer.

Try replacing

UdpSocket::bind("127.0.0.1:23451").ok().expect("fail bind");

with

UdpSocket::bind("127.0.0.1:23451").unwrap()

in order to get a specific error message.

.unwrap() will give you an error message specific to what is actually stopping rust from binding to that port that we can use to diagnose what’s happening.


#9

thread ‘’ panicked at ‘called Result::unwrap() on an Err value: Error { repr: Os(10022) }’,
C:/bot/slave/stable-dist-rustc-win-32/build/src/libcore\result.rs:729


#10

It seems the OS is returning an “InvalidInput” error message, I’m really not sure what that means in your case though. The code works correctly for my machine, I would suggest opening an issue on rust-lang/rust stating that you’re getting WSAEINVAL/10022/InvalidInput error on binding a UdpSocket. I suspect this is some sort of error with rust on windows.


#11

now I have error that:
thread ‘’ panicked at ‘fail bind’, C:/bot/slave/stable-dist-rustc-win-32/b
uild/src/libcore\option.rs:330


#12

what is that: C:/bot/slave/stable-dist-rustc-win-32/b
uild/src/libcore\option.rs:330
? I haven’t that dir…


#13

option.rs is the file which defines the Option type on the rust build machine. The error message is just trying to be helpful in debugging, while not really being helpful. I would suggest opening an issue on the rust github repository as stated earlier.


#14

Thanks, I do it