That seems overly complicated, what are you trying to do?
I *think* your bottle neck is that you're reading 1 byte at a time.
You need to read more, if you simply expand your buffer your code may
work.

The way I've done is is to have a separate thread that's strictly for
TCP communication.  There is no reason to have delays in your code
unless you're trying to throttle it, and even then it's just better to
do it inside a read loop in a separate thread.
So, after reading some bytes in this thread, I simply send these using
a Handler/Message object.
However, at this points the bytes are indeed copied if I'm correct.
If you want to avoid that overhead, you can just use a lock around
your data buffer.

On Tue, Oct 4, 2011 at 7:21 AM, simon <[email protected]> wrote:
> 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



-- 
~ Jeremiah:9:23-24
Android 2D MMORPG: http://solrpg.com/, http://www.youtube.com/user/revoltingx

-- 
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

Reply via email to