C++ implements UDP socket communication.
I don't know how to set the receive buffer size.Do you have any tools to see how much buffers you sent?
The code on the page I referenced when I made it is set to 2048 bits.
Packet transmission and reception over UDP/IP
Environment
Ubuntu 20.04 LTS
Short Answer
The buffer size should be 2048 bytes
Long answer
The maximum size of UDP packets on Ethernet can be determined by the MTU. The maximum MTU value can be 1500 bytes or 1492 or 1454 when PPPoE is included.Therefore, it is safe to set the maximum number of bytes that can be transferred at one time to 1454.
The buffer size should be greater than 1454, so any number is fine.Extremely speaking, it's also ants that have a 1 gigabyte buffer and use only 1454 bytes.It's meaningless, but it's safe to have a power size of 2 to fit well into the CPU's L1/L2/L3 cache boundary (in the hope that the compiler and execution environment will put it on), so it's enough to use it as 2048, which should be 1454 or higher is 2048.
If all equipment is jumbo-framed on the local intranet, you can increase the MTU, but the best value depends on the equipment, so the best value depends on the field.The software creator should not worry about it and adopt MTU=1454 bytes.
If UDP is implemented over Ethernet, UDP can only succeed or fail, so "how much buffer did you send?" is meaningless.From the receiver's point of view, the transfer is either successful or unsuccessful.sendto()
gets exactly the number of bytes sent by sendto()
and nothing (not even known if it succeeds or fails)
The question is ambiguous, so the interpretation may be different.
If possible, it would be better to describe the situation in detail
UDP packet size and maximum message size are determined at the design stage (originally)
If you don't know the structure (details) of the sending program, you can check the contents of the sending.
MSG_PEEK
, MSG_TRUNC
show the packet lengthMSG_PEEK
) Actual LoadAs other solutions show, the design of the receive buffer size is
Also note that recent operating systems including Ubuntu may not be able to send (as is) if the MTU is exceeded.
Note: udp-IPv4 User Datagram Protocol
By default, UDP on Linux performs Path MTU Discovery. In other words, the kernel records the maximum transmission unit (MTU) for a specific destination IP address and returns EMSGSIZE
if UDP packet write exceeds the MTU. Applications should reduce the packet size if EMSGSIZE
is returned. You can also disable Path MTU Discovery using the socket option IP_MTU_DISCOVER
or the /proc/sys/net/ipv4/ip_no_pmtu_disc
file (see ip(7) for more information). If Path MTU Discovery is disabled, UDP fragments the packet if the packet size is larger than the interface MTU. However, disabling Path MTU Discovery is not recommended for performance and reliability reasons.
About the code you used as a reference
It doesn't look like a program to refer to.
recvfrom()
argument from_addr
, sin_size
has not been initialized (sin_size
is for both input and output)memset()
(no need)You should refer to the API documentation and refer to the site where you are using it correctly.
(When I searched for it, there were a lot of NULL specification that I ignored, so I couldn't find any variable specification, but it should be somewhere.)
© 2024 OneMinuteCode. All rights reserved.