I am working on a client-side server application in which the server (a desktop program written in Java) sends screenshots via UDP to my Android device on the same wireless network. Of course, since the datagram size is larger than the standard udp size (65 K), I create pieces of them and loop them to my Android device using UDP.
My problem is that it works great on Samsung Galaxy Note 2 (Android 2.4.2), but it doesn’t work on other Android devices (even devices running the same version for Android. For Galaxy Tab 3, Galaxy Note 1). The code gets stuck in socket.receive (datagram) and doesn't even receive one packet.
Can someone please help me on this ???
public ClientListener(int port, int fps, AppDelegate del){
delegate = del;
framesPerSecond = fps;
try{
serverAddr = getLocalIpAddress();
dgp = new DatagramPacket(buf, buf.length);
}catch (Exception e){
Log.e("ClientActivity", "C: Error", e);
}
serverPort = port;
}
public void run() {
try {
socket = new DatagramSocket(serverPort, serverAddr);
connected = true;
Timer timer = new Timer();
int frames = 1000/framesPerSecond;
Log.e("FRAMES", ""+frames);
timer.scheduleAtFixedRate(getImageTask, 0, frames);
listen();
}
catch (Exception e) {
Log.e("ClientActivity", "Client Connection Error", e);
}
}
private void listen()
{
while(connected){
try{
socket.receive(dgp);
byte[] data = dgp.getData();
System.out.println("data received in datagram clientListener====="+data);
short session = (short) (data[1] & 0xff);
short slices = (short) (data[2] & 0xff);
int maxPacketSize = (int) ((data[3] & 0xff) << 8 | (data[4] & 0xff));
short slice = (short) (data[5] & 0xff);
int size = (int) ((data[6] & 0xff) << 8 | (data[7] & 0xff));
System.out.println("------------- PACKET -------------");
System.out.println("SESSION_START = "
+ ((data[0] & SESSION_START) == SESSION_START));
System.out.println("SSESSION_END = "
+ ((data[0] & SESSION_END) == SESSION_END));
System.out.println("SESSION NR = " + session);
System.out.println("SLICES = " + slices);
System.out.println("MAX PACKET SIZE = " + maxPacketSize);
System.out.println("SLICE NR = " + slice);
System.out.println("SIZE = " + size);
System.out.println("------------- PACKET -------------\n");
if ((data[0] & SESSION_START) == SESSION_START) {
if (session != currentSession) {
currentSession = session;
slicesStored = 0;
imageData = new byte[slices * maxPacketSize];
slicesCol = new int[slices];
sessionAvailable = true;
}
}
if (sessionAvailable && session == currentSession) {
if (slicesCol != null && slicesCol[slice] == 0) {
slicesCol[slice] = 1;
System.arraycopy(data, HEADER_SIZE, imageData, slice
* maxPacketSize, size);
slicesStored++;
}
}
if (slicesStored == slices) {
ByteArrayInputStream bis = new ByteArrayInputStream(
imageData);
Bitmap bp=BitmapFactory.decodeStream(bis);
delegate.getController().setImage(bp);
Log.e("Testing", "Received image");
}