Comments (4)
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.
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.
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.
Thanks!
from socket2.
Related Issues (20)
- msghdr as a private field in MsgHdr/MsgMutHdr HOT 2
- Set Socket interface using interface index HOT 1
- What happened to the `RAW` socket type? HOT 1
- Question: Possible to support architectures that only provide the `std::net` interface (and only TCP)? HOT 4
- Windows: `sa_family_t` should use `ADDRESS_FAMILY` from `windows-sys` HOT 5
- nonblocking connect HOT 6
- Set `ss_len` when creating `SockAddr` from std HOT 1
- Why cannot find WASStartup and WSACleanup in source code? HOT 7
- bind_device succeeds, but subsequent sends fail with no such device or address. HOT 5
- QUESTION: How to special `laddr` when dialing connection? HOT 3
- Android support HOT 3
- don'tassign8080 port
- Currently socket2 has no way to set the DF bit on packets. The attached patch adds it. HOT 3
- Error: BoxSend(Os { code: 98, kind: AddrInUse, message: "Address already in use" }) HOT 2
- How to read EventExtra from the event? HOT 2
- socket() always fails on WSL1 due to unsupported SOCK_CLOEXEC flag HOT 5
- Support for {recv,send}mmsg HOT 2
- New release HOT 1
- socket error HOT 1
- Provide `bind_device_by_index_v{4|6}` methods for linux. HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from socket2.