3

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
jane1012
  • 111
  • 1
  • 13

1 Answers1

4

You shouldn't close streams. Your communication with server closes when you close streams. Usually server communication happens in infinite loop. Something like this:

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];
      while (true) {
         // Read next message.
         bytesToRead = is.read(bbb);
         // handle message...
         // If you need to stop communication use 'break' to exit loop;
      }
      is.close();
      out.close();
      socket.close();

So thread stops on line bytesToRead = is.read(bbb) until server sends any message, after that it handles message any way you want and got back to reading next message. If you need to stop communication exit loop using break.

Mikita Belahlazau
  • 15,326
  • 2
  • 38
  • 43
  • Thank you for the answer! I tried as you tell me but I don't receive nothing more than the first message and "bytesToRead" is always = -1. I read in this answer: [link](http://stackoverflow.com/a/611777/1696856) that "A read of -1 doesn't mean, "there's no data present on this attempt," it means, "the stream is closed and there will never be any data so stop asking." I don't know what I should do! Thanks – jane1012 Nov 22 '12 at 15:08
  • Are you sure server didn't close connection? – Mikita Belahlazau Nov 22 '12 at 15:11
  • Is there a way to know that? with method isConnected()? – jane1012 Nov 22 '12 at 15:15
  • Hm, try it. I don't know :\ – Mikita Belahlazau Nov 22 '12 at 15:18
  • I accepted your question now, I finally found that it was my error: I sent to the server a byteBuffer with one more byte than the length of the message, so the server closed the connection. Thank you for reply – jane1012 Dec 07 '12 at 09:35