Hi, in my application I need to send and receive data bytewise over TCP. At the beginning of development I tried to implement that functionality as resource-efficient as possible. So I finally decided to use Messages (android.os.Message) to cyclically initiate reading bytes from socket. After establishing the connection I assign Input- and OutputStream and order Android to execute the runnable "readFromSocket" after 100ms:
OutputStream out = null; InputStream in = null; Handler readInputHandler; Socket gateway = new Socket(); gateway.connect(new InetSocketAddress(address, port), 20000); if (gateway.isConnected()) { in = gateway.getInputStream(); out = gateway.getOutputStream(); readInputHandler.postDelayed(readFromSocket, 100); } The runnable reads one byte after another and passes it to a parser that tries to assemble the single bytes to an instruction (if the parser is successful I send the instruction encapsulated in a Message to a Controller-Object). At the end it orders its own execution after another 100ms: private Runnable readFromSocket = new Runnable() { byte[] input = new byte[1]; @Override public void run() { try { while (in.available() > 0) { in.read(input); parser.parse(input[0]); } } catch (IOException e) { // InputStream unavailable e.printStackTrace(); } // Initiate reading after 100 ms readInputHandler.postDelayed(this, 100); } }; After testing my app on two different devices (Sony Ericsson Xperia neo and Acer Iconia Tab) I currently hesitate whether that implementation is the best solution... When sending many bytes to the Xperia neo it often takes seconds until my app has received all bytes. In comparison, on the Iconia Tab there isn't such a big delay but the performance also isn't very well. What is the best solution to implement that functionality for an Android-App? Thanks in advance. -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en