GithubHelp home page GithubHelp logo

Comments (4)

Thomasdezeeuw avatar Thomasdezeeuw commented on July 21, 2024

You're using a Vec, with dereferences to a slice (in recv(&mut buffer)), but the Vec is empty and so is the slice. So your doing: recv(&[]) effectively. Try calling buf.resize(buf.capacity(), 0) and then receiving. Also you might want to move the Vec out of the loop, your allocating each iteration.

    let mut buffer = vec![0; 1024 * 1024]; // Allocate a zeroed buffer.
    loop {
       buffer.resize(buffer.capacity(), 0);
        let received_bytes = socket
            .recv(&mut buffer)
            .expect("Unable to receive message!");
        debug!("Received {} bytes", received_bytes);

        // Use the buffer.
       let buffer = &buffer[0..received_bytes];
       // Or change the `Vec` size.
       buffer.resize(received_bytes);
    }

And a final note: you can only receive a single UDP packet per call to recv and a single packet can only be less than 1 << 16 bytes large, so your buffer is too large.

from socket2.

dancojocaru2000 avatar dancojocaru2000 commented on July 21, 2024

Oh damn, I'm amazed I forgot about that! Yeah, Vec is equivalent to C++ std::vector, not std::array. I'm silly. I must have assumed that with_capacity does resize as well, but I forgot that in C++ too that just means making sure the internal buffer is at least that size in order not to require re-allocations when pushing elements.

Speaking of Vec, is there any way to create big stack allocated arrays? As far as I know, "C-like" arrays (dunno how else to describe them) are limited in functionality above 32 elements.

I appreciate the hint about performance and that I'm aware of, but, when messing around with new concepts, I prefer to sacrifice performance in order to have stuff like making sure an array is redeclared and zero-initialised each loop, and also confined to the loop's scope, even if in this case it makes no difference.

Also, I'm essentially a beginner to socket programming, so I didn't know about the size restriction to UDP packets! Thank you!

from socket2.

Thomasdezeeuw avatar Thomasdezeeuw commented on July 21, 2024

You can do the following for a stack backed buffer:

let mut buf = [0; 1 << 16];
socket.recv(&mut buf);

Although it wouldn't implement some useful traits on the array it will likely implement them on the slice, and rustc is usually smart enough to deference the array automatically.

Alternatively you can use a crate like arrayvec or the Vec type from heapless, for a more Vec like API.

from socket2.

dancojocaru2000 avatar dancojocaru2000 commented on July 21, 2024

Thanks!

from socket2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.