开发者

Additional bytes received by a DatagramPacket

I have a problem that I see for the first time ever, I'm using java DatagramSocket (s) for sending and receiving data in my app.

this is how I send the data:

byte[] buffer = "my data".getBytes();
senderPacket = new DatagramPacket(buffer, 0, buffer.length, remoteAddress, remotePort);
socket.send(senderPacket);

now when I sent the byt[] buffer, it's size was 275. but when I received the packet on the receiving app and got the byte[] from the receiverPacket, like this:

buffer = new byte[2048];
receiverPacket = new DatagramPacket(buffer, buffer.length);
socket.receive(receiverPacket);

I found that the received bytes count where 278 bytes that's additional 3 bytes.

This might seem like not a 开发者_开发问答big problem, but actually it is for me, please any idea why is this happening, and how to solve it, any logical explanation would be helpful.

thanks


The 3 extra bytes could be a length marker. I know that when I do sockets in C/C++, I typically dedicate the first 4 bytes to sending the length of my message, and then I use that data to determine how much to read in order to read the entire message.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜