Udp socket error


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();
    let x = input.trim().to_string().parse::<i32>().unwrap();
    return x;
fn main() {
    let port = 12345;
    let mut socket = UdpSocket::bind("");
if socket.is_err() {
    let err = socket.is_err();
    println!("failed to read from stdin : {}", err);
    let y = in_i32();
let mut buf = [0,3];
for i in 0..buf.len(){
socket.unwrap().send_to(&[7], ("", 23451)).unwrap();
let y = in_i32();


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

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

    let x = ask_number();
    let buf = x.into_bytes();
    socket.send_to(&buf, "").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.


crash then I write this

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


Which rustc’s version are you using?

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


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


Is this protocol valid?




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

Try replacing

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



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.


thread ‘’ panicked at ‘called Result::unwrap() on an Err value: Error { repr: Os(10022) }’,


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.


now I have error that:
thread ‘’ panicked at ‘fail bind’, C:/bot/slave/stable-dist-rustc-win-32/b


what is that: C:/bot/slave/stable-dist-rustc-win-32/b
? I haven’t that dir…


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.


Thanks, I do it