Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7035843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:21:08+00:00 2026-05-28T01:21:08+00:00

I have a PC server and an android client; my android client start a

  • 0

I have a PC server and an android client; my android client start a socket connection to server.

While I am connected to server, I also receive data from server to android client;

Here is my code:

Socket socket = null;
DataOutputStream out = null;
DataInputStream in = null;
InputStream inputStream = null; 
OutputStream outputStream = null;
...

public void connectToTCP()
        {

            try
            {   
               socket = new Socket(HOST_ADDRESS, PORT);

                socket.setSoTimeout(30000);
                outputStream = socket.getOutputStream();
                out = new DataOutputStream(outputStream);
                inputStream = socket.getInputStream();
                in = new DataInputStream(inputStream);

                Log.e("TCP-", "Connected");

               while (socket.isConnected()){readBytes();}
            } 
            catch (UnknownHostException e) 
            {

               Log.e("Error in tcp connection","Unknown Host");

            }
            catch (IOException e) 
            {

                Log.e("Error in tcp connection", "Couldn't get I/O for the connection");

            }
        }

    public void readBytes() throws IOException
        {   
            if (in.available() > 0)
            {
                byte[] buffer = new byte[in.available()];
                if (buffer.length > 0)
                {
                    if (mListener != null)
                    {
                        int numberOfBytes = in.read(buffer);
                        mListener.tcpConnectionDataReceived(buffer, numberOfBytes);
                    }
                }
            }
        }

but my problem is in performance. I tested the code on the device and I noticed (from task manager) that the app consume a lot of resources (CPU usage is more than 50%) but when I stop reading from socket by deleting this while loop while (socket.isConnected()){readBytes();} CPU usage becomes less than 1%.

Any ideas to solve this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:21:09+00:00Added an answer on May 28, 2026 at 1:21 am

    You readBytes() method will return immediately if no data is available. Since it’s in a tight loop, you’re essentially continuously checking if there is something available, wasting a lot of CPU power.

    With the code you show, you would be better off doing a plain blocking read (i.e. remove the available() check altogether, and use a reasonable, fixed-size buffer).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written simple client server socket programming code as follow MyServer.java import java.io.DataInputStream;
I have a simple client-server app on android. the android service communicates with the
I have a server that sends data via a socket, the data is a
I am newbie to android. I have client server based application. Server keeps on
I have a Server Socket and 3-4 android devices as clients. I'm using TCP/IP
I have WCF RESTful service and Android client. Server replies with 400 when I
I have a client on Android and server on c#. The client sends messages
I have an Android client and a C# server. They are communicating over sockets
I have to implement a client-server architecture where there are many android client located
I have client/server data passing all working correctly. Text, Images, etc. My users create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.