Reputation: 315
I'm trying to implement data transfer from a Notebook (Linux) to an Android Device over UDP.
On the notebook I launch:
cat /home/me/my/file.txt | nc -u 192.168.150.3 12345
Or:
nc -u 192.168.150.3 12345 < /home/me/my/file.txt
getting same result.
On the Android Device my code is:
byte[] msg = new byte[100000];
DatagramPacket p = new DatagramPacket(msg, msg.length);
DatagramSocket s = new DatagramSocket(portNumber);
s.receive(p);
message = new String(msg, 0, p.getLength());
s.close();
If I set a breakpoint on the line with "message =" I can see msg.length = 100000. That's right. But p.getLength() is only 2048. That means only a part of my data is transferred. I mean "message" doesn't contain all data which is in "file.txt". Why does it happen? What do I do wrong?
Upvotes: 0
Views: 1351
Reputation: 123531
nc does not send the whole file in a single UDP packet, so don't expect to get it in a single call to receive. You must call receive multiple times until everything is complete. Of course, with UDP you get no "end of connection", so you must have some other indicator that you got all data. And, with UDP packets can be silently lost or reordered, so there is no guarantee that you get the file the way you want.
Upvotes: 1