How to read data from socket if new data arrives

Tutorials with advanced 'difficulty' and more Lines of Code.

How to read data from socket if new data arrives

Postby squinx22 » Tue Mar 15, 2011 6:28 am

Hi Android Developers,

I have a question in socket programming in android. I have an application that communicates to a server using iq stanzas. I want my application to READ ONLY the socket's inputstream when NEW DATA is available for fetching. Currently, what I did is, I create a timer that scheduled for the reading of socket's inputstream every 1second. This is not that good because it will end up of reading the socket even if there's no available data. And it drains the battery fast.

Is there an API for android that will tell or send a flag to the client that is connected to a socket whenever a new data is available so that it is the only time I will read the inputstream?

Thank you in advance... :D
squinx22
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Apr 25, 2008 2:56 am

Top

Re: How to read data from socket if new data arrives

Postby ruelblapid » Tue Mar 15, 2011 7:08 am

Hi squinx22,

I believed you should develop this one your self on how your server and client communicates like for example create a message format that only client connected can understand,persistent connection is needed for this and use unique port, there's no flag or API in java the way you wanted.

I suggest you need to use observer pattern if you wanted to make it much organize.

If you have additional questions just let me know.



Best,
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Re: How to read data from socket if new data arrives

Postby squinx22 » Tue Mar 15, 2011 7:13 am

Thank you ruelblapid! :)
squinx22
Junior Developer
Junior Developer
 
Posts: 12
Joined: Fri Apr 25, 2008 2:56 am

Re: How to read data from socket if new data arrives

Postby ruelblapid » Tue Mar 15, 2011 7:28 am

You're welcome dude.

Best,
Ruel B. Lapid
Senior Android Engineer
User avatar
ruelblapid
Experienced Developer
Experienced Developer
 
Posts: 65
Joined: Mon Mar 14, 2011 2:37 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests