how to tell a DatagramPacket's actualy size
Author |
Message |
greenapplesodaex
![](http://img.photobucket.com/albums/v235/greenapplesodaex/big.jpg)
|
Posted: Sat Nov 15, 2008 11:17 am Post subject: how to tell a DatagramPacket's actualy size |
|
|
I'm doing this assignment where you will continously receive 2 types of datagram packets in any order.
the packets have different sizes (8 or 20 bytes), and doesnt seem to have data we can use as headers, is there a way to tell them apart?
because we dont know which size it will be, we have to allocate a byte[] with the max size of the packet. the packet either gets filled up, or have 12 bytes left. but the rest bytes gets filled with junk, the junk ranges from seemingly "valid" number to something like -140000
getLength() for the packet only gives the byte[] allocated to it, which is useless, since it's always gonna be the max size
code looks something like this
code: |
byte[] receiveData=new byte[44];
try
{
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
routerSocket.receive(receivePacket);
packetSize=receivePacket.getLength();
System.out.println("length is "+packetSize);
return receivePacket.getData();
}
catch (Exception e)
{
//System.out.println("");
}
|
im at loss of idea here....
so thanks in advance! |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
greenapplesodaex
![](http://img.photobucket.com/albums/v235/greenapplesodaex/big.jpg)
|
Posted: Sat Nov 15, 2008 12:47 pm Post subject: RE:how to tell a DatagramPacket\'s actualy size |
|
|
actually... nvm, there was a packet we get before this (and we can be sure the order and of that one) and that returned a whole bunch of garbage after the real data. it always filled up the whole thing and my observation was only based on that. the other 2 packets doesnt seem to do contain garbages, so everything works fine now |
|
|
|
|
![](images/spacer.gif) |
|
|