I'm a newbie so I'm sorry if I ask dumb questions.
I have to do a socket connection with a server.
The client (the android device) has to send a message to the server and to wait for an ack message. Then the server, if it doesn't receive nothing from the client for 30 seconds, sends a request for an ack message.
What I've done it's the first part: the client sends the message and read the response.
What I ask it is: How can the client wait for a possible message from the server?
This is the code I wrote:
In onCreate method:
Thread myThread = new Thread(new myRunnable());
myThread.start();
And this is myRunnable:
public class myRunnable implements Runnable{
public void run(){
try{
Socket socket = new Socket(serverIp, serverPort);
InputStream is = socket.getInputStream();
OutputStream out = socket.getOutputStream();
out.write(byteBuffer.array());
out.flush();
byte[] bbb = new byte[1024];
// Receive message from server
int bytesToRead= is.read(bbb);
is.close();
out.close();
socket.close();
Successives read operations return -1, so how can I stay waiting for a server message?
Thank you very much for any advices.