Socket Client read reply hangs

innospiring
Posts: 3
Joined: Sun Jan 29, 2017 10:20 am

Socket Client read reply hangs

Postby innospiring » Thu Dec 14, 2017 5:45 pm

I am using the SocketClient example of Kolban and try to read reply from SocketServer
here is the snippet

char *data = "Hello world";
rc = send(sock, data, strlen(data), 0);
ESP_LOGE(tag, "send: rc: %d", rc);

vTaskDelay(1000/portTICK_PERIOD_MS);

/* Check answer */
int ret = 0;
int cnt = 0;
do {
/* Reset buffer */
memset(data_buffer, 0x00, sizeof(data_buffer));
/* Read socket */
ESP_LOGE(tag, "read");
ret = read(sock, data_buffer, 1000);
ESP_LOGE(tag, "ret:%d cnt %d",ret,cnt);
cnt++;
} while(ret != 0 && cnt < 1000);


and here the snippet from Android:

private class SocketServerThread extends Thread {

int count = 0;

@Override
public void run() {
try {
serverSocket = new ServerSocket(SocketServerPORT);
while (true) {

if (count%4 == 0) message = "";
Socket socket = serverSocket.accept();


BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
String data = "";
while ((line = in.readLine()) != null) {
Log.d("line of data ", line);
data += line + "\n";
}

int messageLength = data.length();

SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(socket, messageLength);
socketServerReplyThread.run();

count++;
String lString = String.format("%d",messageLength);

message += lString + " bytes#" + count + " from " + socket.getInetAddress() + ":" + socket.getPort() + "\n";

updateInfoLabel();


}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

private class SocketServerReplyThread extends Thread {

private Socket hostThreadSocket;
int cnt;

SocketServerReplyThread(Socket socket, int c) {
hostThreadSocket = socket;
cnt = c;
}

@Override
public void run() {
OutputStream outputStream;
String msgReply = "#" + cnt;

try {
Log.d(TAG, String.format("sending reply to client %s",msgReply));
outputStream = hostThreadSocket.getOutputStream();
PrintStream printStream = new PrintStream(outputStream);
printStream.print(msgReply);
printStream.close();

Log.d(TAG, "message was sent");

message += "=>" + msgReply + "\n";
updateInfoLabel();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
message += "Something wrong! " + e.toString() + "\n";
}
updateInfoLabel();

}

}

when using the debugger I see that both hang in the read statement
ESP32: ret = read(sock, data_buffer, 1000);
Android: while ((line = in.readLine()) != null) {

any ideas?

Who is online

Users browsing this forum: Bing [Bot] and 320 guests