我有一个Android应用程序,它监视UDP数据包并调用一个方法来处理收到的消息。我有一个问题,如果传入的消息更长,它将调用方法来处理消息。但是如果传入的消息较短,则不会调用该方法,但如果我发送短消息两次,它将被处理或在长消息后立即发送短消息,它将被处理。我该如何解决?我需要处理短消息和长消息。
UDP接收代码,用于调用长消息的方法,但不能用于短消息;
class Server extends Thread {
@Override
public void run() {
try {
int port = 8888;
// Create a socket to listen on the port.
dsocket = new DatagramSocket(port);
// Create a buffer to read datagrams into.
byte[] buffer = new byte[100];
// Create a packet to receive data into the buffer
DatagramPacket packet = new DatagramPacket(buffer, uffer.length);
while (true) {
try {
// Wait to receive a datagram
dsocket.receive(packet);
//Convert the contents to a string,
String msg = new String(buffer, packet.getOffset(), packet.getLength());
if (msg !=null)
{
call(msg);
msg=null;
}
// Reset the length of the packet before reusing it.
packet.setLength(buffer.length);
} catch (Exception e) {
dsocket.close();
break; }
}
} catch (Exception e) {
dsocket.close(); e.printStackTrace();}
}
}
public void call(String mg) {
int v;
int sq=0; // store squence no of the calls
int indxs=0; // start alarm msg index
boolean fnd; // alarm msg filter flg
char Sqchar1; // squnce no of the call
char Sqchar2; // squnce no of the call
char Sqchar3; // squnce no of the call
int pospri; // default position 8 of priority for one digit seqence no
String pstr = null; // alarm msg string without filter
boolean aflg =false; // alarm msg found flg becomes true
boolean retval = false; // stop same active sequence msg
int sqnum; // defualt sq to one digit
int lstr = mg.length();
int indxe= lstr; // end alarm msg index
// typica string mg = 145 ($\M A 1 5 "bla bla")// valid alarm msg
// typical string mg = 88 ($\M A 12 6 "bla bla")// valid alarm msg
// typical string mg = 127 ($\M A 123 1 "bla bla")// valid alarm msg
// typical string mg = 1111 ($\M A 130 2 "bla bla")// valid alarm msg
// typical string mg = 148 ($\M N 1)// valid CANCEL msg
// typical string mg = 90 ($\M N 12)// valid CANCEL msg
// typical string mg = 130 ($\M N 123)// valid CANCEL msg
// typical string mg = 1115 ($\M N 130)// valid CANCEL msg
// NON Valid string mg = 456
// NON Valid string mg = 113 ($\C x n " bla bla")
//serach for alarm string
for(int i=0; i< lstr; i++)
{
char schar = mg.charAt(i);
if(schar=='$')
{
rHandler.post(rRunnable);
pstr = mg.substring(i, (lstr-1));
aflg =true; break;
}
}
if(aflg)
{
char achar = pstr.charAt(2); // indicateas alarm msg
char ACchar = pstr.charAt(4); // indicateas alarm active
if(achar =='M' && ACchar == 'A')
{
Sqchar1= pstr.charAt(6);
if(pstr.charAt(7)!=' ')
{
sqnum=2;
Sqchar2= pstr.charAt(7);
pospri = 9;
}
else
{
sqnum=1;
Sqchar2=' ';
pospri = 8;
sq = Character.getNumericValue(Sqchar1);
}
if(sqnum==2)
{
if(pstr.charAt(8)!=' ')
{
//squnum =3
pospri = 10;
Sqchar3= pstr.charAt(8);
int sq1 = Character.getNumericValue(Sqchar1);
int sq2 = Character.getNumericValue(Sqchar2);
int sq3 = Character.getNumericValue(Sqchar3);
sq= (sq1 * 100) + (sq2 * 10) + sq3;
}
else
{
int sq1 = Character.getNumericValue(Sqchar1);
int sq2 = Character.getNumericValue(Sqchar2);
sq= (sq1 *10) + sq2;
pospri = 9;
}
}
int plen = pstr.length();
fnd=false;
for(int x=0; x<plen; x++)
{
char ichar = pstr.charAt(x);
if( ichar== '"' && !fnd )
{
indxs = x+1; fnd = true;
}
else
if(ichar == '"' && fnd)
{
indxe = x; break;
}
}
String amsg = pstr.substring(indxs, indxe);
// stop repetated msgs
if(sqlist[sq]== sq)
{
retval = true;
}
if (!retval)
{
sqlist[sq] = sq;
alarmlist[sq] = amsg;
clrlist[sq]=Character.toString(pstr.charAt(pospri));
sound(clrlist[sq]); tHandler.post(tRunnable);
}
}
else
if(achar =='M'&& ACchar == 'N')
{
int clen =pstr.length();
if(clen ==7 )
{
Sqchar1= pstr.charAt(6);
sq= Character.getNumericValue(Sqchar1);
}
else
if(clen ==8 )
{
Sqchar1= pstr.charAt(6);
Sqchar2= pstr.charAt(7);
int sq1 = Character.getNumericValue(Sqchar1);
int sq2 = Character.getNumericValue(Sqchar2);
sq= (sq1 * 10) + sq2;
}
else
if(clen ==9 )
{
Sqchar1= pstr.charAt(6);
Sqchar2= pstr.charAt(7);
Sqchar3= pstr.charAt(8);
int sq1 = Character.getNumericValue(Sqchar1);
int sq2 = Character.getNumericValue(Sqchar2);
int sq3 = Character.getNumericValue(Sqchar3);
sq= (sq1 * 100) + (sq2 * 10) + sq3;
}
sqlist[sq] = 0;
alarmlist[sq] = null;
clrlist[sq]= null; cHandler.post(cRunnable);
}
else
{
return;
}
list.clear();
int elementsCount = alarmlist.length;
v=0;
for(int i=0; i < elementsCount; i++)
{
String disp =alarmlist[i];
if(disp != null)
{
list.add(v, disp+ "#" + clrlist[i]);
v++;
}
}
write(list, "/mnt/sdcard/msg.tmp"); dHandler.post(dRunnable);
} //alarm msg
return;
}
答案 0 :(得分:0)
我发现了问题。这段代码与此无关。这与我发送UDP数据包的方式有关。我写了一个C ++程序来发送UDP数据包,当我发送较短的消息时,我有一个错误。一旦我修复它,该应用程序工作正常。