jeudi 14 juillet 2016

How to handle tcp inputstream correctly - Java/Android

I'm creating an android application that needs a permanent TCP-Connection to a Server.

I've created a Service that establishes the Connection and listens for incoming Bytes on the Inputstream (The service runs in the background).

public  class TCPServiceConnection extends Service{
        //variables......
        //...............
        public void onCreate() { 
               establishTCPConnection(): 
        }

The first 4 incoming bytes symbolize the message-length of a complete Message.

After reading a complete Message from the Inputstream into a separate buffer, I want to call another Service/Asynctask in a separate Thread that analyses the Message. (The service should continue listening for further incoming messages).

        public handleTCPInput() {
               while(tcp_socket.isConnected()) {
               byte[] buffer = readCompletemessagefromTCPInputstream; 
               calltoAnotherThreadToanalyzeReceivedMessage(buffer); 
               }
               //handle exceptions.......
        }

Is there an existing Messagequeue-system in Android/Java that already handles the multi-access onto my separated byte[] buffer ?

Aucun commentaire:

Enregistrer un commentaire