title: Is there any callback mechanism in android when there is data available to read in socket
body: <p>Well I am familiar with socket programming in c, iOS environment..But now trying to connect my android and my remote server via sockets...As a start up, I wrote a simple server program in C and ran it on my desktop which patiently wait for a connection request, accepts connection, then wait for some request string, and on getting the request string returns some response string, then again wait for next request and go on..Well you get the idea..</p>
  
  <p>So far </p>
  
  <ol>
  <li>I have established a connection with my android and server</li>
  <li>sent and received data</li>
  </ol>
  
  <p>And this is my client code..</p>
  
  <pre><code>public class SocketMaster {
      private Socket clientSocket     =   null;
          BufferedReader socketReadStream =   null;
      public boolean connectToHost(String ip, int port){
          try {
              InetAddress serverAddr  =   InetAddress.getByName(ip);
              try {
                  clientSocket        =   new Socket(serverAddr, port);
                  socketReadStream    =   new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                  String line     =   null;
                  String stringToSend =   "This is from client ...Are you there???";
  
                  //Write to server..stringToSend is the request string..
                  this.writeToServer(stringToSend);
  
                  //Now read the response..
                  while((line = socketReadStream.readLine()) != null){
                      Log.d("Message", line);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
                  return false;
              }
          } catch (UnknownHostException e) {
              e.printStackTrace();
              return false;
          }
          return true;
      }
  
      public boolean writeToServer(String stringToSend){
          DataOutputStream dos        =   null;
          try {
              dos                         =   new DataOutputStream(clientSocket.getOutputStream());
          } catch (IOException e) {
              e.printStackTrace();
              return false;
          }
          try {
              dos.write(stringToSend.getBytes());
          } catch (IOException e) {
              e.printStackTrace();
              return false;
          }
          return true;
      }
  }
  </code></pre>
  
  <p>I create an object of SocketMaster from another activity and calls connectToHost passing ip and port of server..Everything works fine and I am able to connect, send and receive data..</p>
  
  <p>Now my question is regarding these lines</p>
  
  <pre><code>//Now read the response..
  while((line = socketReadStream.readLine()) != null){
       Log.d("Message", line);
  }
  </code></pre>
  
  <p>From what I understand, the thread that executes this code</p>
  
  <ul>
  <li>blocks until there is data to read</li>
  <li>if data available, read it, then because of loop again blocks until
  next chunk of data arrives</li>
  </ul>
  
  <p>Simply I can't run this code on main thread as it will block my UI Activities..I can think of running this in a background thread, which is one option...</p>
  
  <p>But Ideally what I want is... </p>
  
  <p>A <strong>callback</strong> (I think, listener in android terms, anyone familiar with iOS <a href="http://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFSocketRef/Reference/reference.html">CFSocket</a> and <a href="http://developer.apple.com/library/ios/#documentation/CoreFoundation/Reference/CFSocketRef/Reference/reference.html">callback</a>),method , which gets called when there is data available to read, so that I can just read data and then return..</p>
  
  <p>Is there any such mechanism in android/ java...If not ( :-( ), can anyone link me to some example where socket handling is done in background...</p>
  
  <p><strong>EDIT:</strong> 
  I was not asking about creating a callback mechanism myself..I was just asking is there any listener present which notify you when there is data to read, when the connection got disconnected..Thus I can eliminate the above given while loop...If there isn't any callback, I am ready move this socket to another thread, which loops arounds and reads data (which infact I have already done)...</p>
  
  <p>**</p>
  
  <h2>EDIT Bounty time again..</h2>
  
  <p>**</p>
  
  <p>Well I am 2 weak old android/java developer with a good 3.5 years of c/c++/objective c development experience behind that... So I have seen low level c native sockets (which blocks(configurable) when use read() and recv()) and apple's CFSocket (which is C socket itself but provide a wrapper to provide callback mechanism).. <strong>I am not asking about coding a callback mechanism myself here</strong> (which I am whole heartedly ready to do if there is no readymade alternative present, but why reinvent the wheel again?)...I am beginning to understand java as very advanced and high level platform.. I thought there should be some higher level wrapper library lurking around..So I am thinking of increasing the audience to this thread by starting a bounty..  </p>