问题
I am trying to set/increase the receive buffer size of a datagram socket. I would like to do this since I am experiencing some random packet loss when sending data between a PC and the Android device which lie on the same local network and I would like to test if increasing the buffer size would have any effect in reducing this UDP packet loss. I am trying to set the buffer size using the below code.
DatagramSocket socket = new DatagramSocket();
socket.setReceiveBufferSize(655360);
and then later on (before I start reading from the socket) I check the receive buffer size as follows:
Log.i(TAG, "Size: " + socket.getReceiveBufferSize());
However, the log message always displays a buffer size of 163840. What is the problem with this code? Should I set the receive buffer size in another way.
Thanks
回答1:
The log message always displays a buffer size of 163840.
That's what the platform gave you.
What is the problem with this code?
Nothing. You need to read the Javadoc. The platform may adjust the value you supply up or down. "Because SO_RCVBUF
is a hint, applications that want to verify what size the buffers were set to should call getReceiveBufferSize().
"
You don't need a socket receive buffer of 640k. Packet loss happens mostly in the network, and only at the target if you are slow reading.
来源:https://stackoverflow.com/questions/22778823/android-datagramsocket-receive-buffer-size