UDP buffer sizes

Hi, just starting out with Rust and obviously struggling with syntax a bit but making progress. I'm re-implementing a C program and one of the things I need to do is set the read an write buffer sizes for the UDP exchanges but they don't seem to have been exposed in the API. Any suggestions?

What specific C APIs are you trying to port that you can't find a rust equivalent to?

You pass a buffer into calls to the socket so you control the size of that buffer. Are you talking about the OS's internal buffers? I'm not sure why the size of those would matter all that much to application code.

The protocol limits the maximum size. Using that for the read buffer size would be a safe choice.

1 Like

If something isn't available in libstd API, you can get the file descriptor out of a Rust socket, and use the C APIs with it:


This is the C code for the buffers which are set to 192k. I only know that things don't work if I don't do that.
// Set send buffer size
if (setsockopt(sd, SOL_SOCKET, SO_SNDBUF, (const char*)&sendbuff, sizeof(sendbuff)) == -1) {
printf("Failed to set option SO_SNDBUF!\n");
return FALSE;
// Set receive buffer size
if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, (const char*)&recvbuff, sizeof(recvbuff)) == -1) {
printf("Failed to set option SO_RCVBUF!\n");
return FALSE;

The socket2 library has the necessary method and can borrow or convert between its and std types.

1 Like

Thanks. I will take a look at socket2.

...that looks suspicious. Would you mind posting the definition of sendbuff and the code that initializes it?

It looked suspect to me as well but it does work. sendbuff is just an integer. The setsockopt wants a pointer to it and size of it.

1 Like