I am not getting message from server after sending the command to receive the data. The line after inputstream.read
does not execute.
This is the runnable code for getting the message from server.
public class ReceiveRunnable implements Runnable {
private Socket sock;
private InputStream input;
public ReceiveRunnable(Socket server) {
sock = server;
try {
input = sock.getInputStream();
} catch (Exception e) {
}
}
@Override
public void run() {
Log.d(TAG, "Receiving started");
while (isConnected()) {
if (!receiveThreadRunning)
receiveThreadRunning = true;
startTime = System.currentTimeMillis();
try {
byte[] data = new byte[50];
//Read the first integer, it defines the length of the data to expect
input.read(data, 0, data.length);
int length = data[2];
String str = new String(data, "UTF-8"); // for UTF-8 encoding
mList.OnDataIncoming(str);
stopThreads();
} catch (Exception e) {
if (mList != null)
mList.OnConnectionerror();
Disconnect(); //Gets stuck in a loop if we don't call this on error!
}
}
receiveThreadRunning = false;
Log.d(TAG, "Receiving stopped");
}
}