Reputation: 1498
Always i can get the first udp packet, but when i will receive the second udp packet the DatagramPacket
always getting part of the last packet.
for example: I need receive in first packet: "AAAAAAAAAA" I receive: "AAAAAAAAAA"
I need receive in the second packet: "BBBB" I'm receiving: "BBBBAAAA".
How can i flush,erase or another method to solve my problem?
Here is my code:
public DatagramServer(Context c, Activity a) throws SocketException {
setSocket(new DatagramSocket(Constantes.UDP_PORT));
getSocket().setBroadcast(true);
getSocket().setReuseAddress(true);
setContext(c);
setActivity(a);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
receivePacket();
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
public void receivePacket() throws IOException {
byte[] buf = null;
buf = new byte[Constantes.PACKETSIZE];
final DatagramPacket packet = new DatagramPacket(buf, buf.length);
while (true) {
getSocket().receive(packet);
Handler mainHandler = new Handler(getContext().getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
byte[] resposta = packet.getData();
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
if (resposta[0] == 111) {
if (GlobalSingleton.getInstance().is_pode_reconhecer_central()){
final CentralDAO cDAO = new CentralDAO(getContext());
final Central central = new Central();
Central centralExistente = new Central();
Boolean bcentralExiste = false;
central.set_IP(packet.getAddress().getHostAddress());
central.set_mac(CentralService.getMAC(packet.getData()));
central.set_modificado(true);
central.set_ativo(true);
}
else {
centralExistente.set_IP(packet.getAddress().getHostAddress());
centralExistente.set_ativo(true);
for (int i=0;i<lvCentrais.getCount();i++){
Central centrallv = (Central) lvCentrais.getItemAtPosition(i);
if (centrallv.get_mac().equals(centralExistente.get_mac())){
centrallv.set_IP(centralExistente.get_IP());
centrallv.set_ativo(centralExistente.is_ativo());
}
}
}
GlobalSingleton.getInstance().set_pode_reconhecer_central(false);
}
} else {
String respostaString = "";
try {
respostaString = new String(packet.getData(),"ASCII");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}//new String(packet.getData(),packet.getOffset(),packet.getLength());//
if (respostaString.toUpperCase().contains(Constantes.ANSWER_DEVICE)) {
if (GlobalSingleton.getInstance().is_pode_reconhecer_dispositivo()){
final GeradorDAO gerDAO = new GeradorDAO(getContext());
final DispositivoDAO dDAO = new DispositivoDAO(getContext());
List<Gerador> list = gerDAO.selectAll();
if (list.size()>0){
int mac,versao,tipo;
String values[] = Utils.getValorString(respostaString, Constantes.P_COMA, Constantes.P_INDICADOR);
mac = Integer.parseInt(values[0].trim());
tipo = Integer.parseInt(values[1].trim());
versao = Integer.parseInt(values[2].trim());
if (dDAO.selectOne(mac, GlobalSingleton.getInstance().get_current_central().get_id()) == null){
final Gerador gerador = gerDAO.selectOne(tipo, versao);
final Dispositivo dispNovo = new Dispositivo(GlobalSingleton.getInstance().get_current_central().get_id());
dispNovo.set_mac(mac);
dispNovo.set_tipo(tipo);
dispNovo.set_versao(versao);
dispNovo.set_nome(gerador.get_nome_dispositivo());
}
else{
Toast.makeText(getContext(), "Dispositivo já existe", Toast.LENGTH_SHORT).show();
}
}
GlobalSingleton.getInstance().set_pode_reconhecer_dispositivo(false);
}
}
}
}
};
mainHandler.post(myRunnable);
}
}
I'm beginner in android, please if you can help me i will be thankful!
Upvotes: 1
Views: 107
Reputation: 310980
You're ignoring the length in the DatagramPacket.
central.set_mac(CentralService.getMAC(packet.getData()))
should be something like
central.set_mac(CentralService.getMAC(packet.getData(), packet.getOffset(), packet.getLength()))
assuming the getMac()
method accepts those parameters. Otherwise you will have to create an appropriately sized byte array yourself.
EDIT I don't intend to repeat that for every other use of packet.getData().
Upvotes: 1