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 (on success,
I send it encapsulated in a Message to a Controller-Object). Finally
the runnable 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) {
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 my
implementation is the best solution...
When sending many bytes to the Xperia neo, seconds elapse between
sending and receiving/processing those data.
In comparison, on the Iconia Tab there arent't such big delays but the
performance also isn't very good.
How would you implement that functionality?
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en