1

I'm facing UDP packet loss problem based on Android devices. I have two devices. Following code works correctly on one device. The other device lost many packages. I have already read solution of similar problem. In this solution, setting the datagram socket size to 64k is suggested. But I couldn't set it.

How can I change datagram buffer size?

My code:

DatagramSocket udpSocket = null;
try {
    udpSocket = new DatagramSocket(5004);
    udpSocket.setReceiveBufferSize(64*1024);
    Log.d("UDPSocket", "Buffer Size : " + udpSocket.getReceiveBufferSize());
} catch (SocketException e1) {
    e1.printStackTrace();
}

Log:

05-14 10:34:05.960: D/UDPSocket(28021): Buffer Size : 112640
Community
  • 1
  • 1
ilkerulusoy
  • 102
  • 2
  • 9
  • 1
    I don't know what you have to program and why you use udp sockets but if packetloss is a problem for you then don't use udp sockets. With Udp you can never be sure that all packages reach their target and that they are in the order that they have been sended. – user3469517 May 14 '14 at 09:02
  • What can i use? server is sending packets to my devices via rtp as unicast. – ilkerulusoy May 14 '14 at 10:19

2 Answers2

0

Author of the chosen anwser seems to have problems using past tense and speak to present nearly all time, but at one point he exactly say

I removed this code setting buffer size and then it strated receving all the packets

So in fact it was changing datagram buffer size wich seems to have caused it's problem.

By the way, your method to set buffersize probably work, in fact log message respond to you with your platform buffer size, wich you can't change, see Android DatagramSocket receive buffer size.

Community
  • 1
  • 1
GaelFG
  • 81
  • 4
  • I watched all network packets via network shark software. Packets seem right. But i'm facing with lossing packet on my android application. This occurs on same android devices. For example, Samsung Tab 2 and Nexus 4 run correctly. – ilkerulusoy May 14 '14 at 10:37
  • Sorry I was only able to anwser what seemed to be the question to me : How can I change datagram buffer size? i have no clues about your underlying problem of packet loss. But someone pointed that trying to manually set buffersize could cause problems. – GaelFG May 14 '14 at 13:25
0

I resolved my problem. I changed my receive data code.

Past code:

byte[] receiveData = new byte[1328];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
    udpSocket.receive(receivePacket);
} catch (IOException e) {
    e.printStackTrace();
}

New code:

ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.fromDatagramSocket(udpSocket);
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
FileInputStream fin = new FileInputStream(fileDescriptor);

byte[] receiveData = new byte[1328];
int readByte = fin.read(receiveData);
ilkerulusoy
  • 102
  • 2
  • 9