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

  • Home
  • SEARCH
  • 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 7857299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:59:55+00:00 2026-06-02T20:59:55+00:00

I have the following code to run a client socket connection in a asynctask:

  • 0

I have the following code to run a client socket connection in a asynctask:

@Override
protected Boolean doInBackground(Void... params) { //This runs on a different thread
    boolean result = false;
    try {
        Log.i("AsyncTask", "doInBackground: Creating socket");
        SocketAddress sockaddr = new InetSocketAddress("192.168.1.115", 9090);
        nsocket = new Socket();
        nsocket.connect(sockaddr, 5000); //10 second connection timeout
        if (nsocket.isConnected()) { 
            nis = nsocket.getInputStream();
            wr = new BufferedWriter(new OutputStreamWriter(nsocket.getOutputStream()));
            Log.i("AsyncTask", "doInBackground: Socket created, streams assigned");
            Log.i("AsyncTask", "doInBackground: Waiting for inital data...");
            sockState = true;
            byte[] buffer = new byte[4096];
            int read = nis.read(buffer, 0, 4096); //This is blocking
            while(read != -1){
                byte[] tempdata = new byte[read];
                System.arraycopy(buffer, 0, tempdata, 0, read);
                publishProgress(tempdata);
                Log.i("AsyncTask", "doInBackground: Got some data");
                read = nis.read(buffer, 0, 4096); //This is blocking
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        Log.i("AsyncTask", "doInBackground: IOException");
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("AsyncTask", "doInBackground: Exception");
        result = true;
    } finally {
        try {
            nis.close();
            wr.close();
            nsocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("AsyncTask", "doInBackground: Finished");
    }
    return result;
}

@Override
protected void onProgressUpdate(byte[]... values) {
     Log.d("KMC.NetworkTask", String.valueOf(values[0]));
    if (values.length > 0) {
        Log.d("KMC.NetworkTask", "onProgressUpdate: " + values[0].length + " bytes received.");
        result = new String(values[0]);
    }
}

THe socket does work. However, onProgressUpdate is not called, even though the background task tells me data came in.

Anyone got some pointers for me?? I couldn’t find anything on google 😐

  • 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-06-02T20:59:57+00:00Added an answer on June 2, 2026 at 8:59 pm

    For now i solved it like this:

    @Override
    protected Boolean doInBackground(Void... params) { //This runs on a different thread
        try {
            Log.i("AsyncTask", "doInBackground: Creating socket");
            SocketAddress sockaddr = new InetSocketAddress("192.168.1.115", 9090);
            nsocket = new Socket();
            nsocket.connect(sockaddr, 5000); //10 second connection timeout
            if (nsocket.isConnected()) { 
    
                in = new BufferedReader(new InputStreamReader(nsocket.getInputStream()));
                wr = new BufferedWriter(new OutputStreamWriter(nsocket.getOutputStream()));
                Log.i("AsyncTask", "doInBackground: Socket created, streams assigned");
                sockState = true;
    
                String inputLine = "";
                while ((inputLine = in.readLine()) != null) {
                    result = inputLine;
                    Log.d("AsyncTask", "doInBackground: Got some data:" + inputLine);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.i("AsyncTask", "doInBackground: IOException");
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("AsyncTask", "doInBackground: Exception");
        } finally {
            try {
                nis.close();
                wr.close();
                nsocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.i("AsyncTask", "doInBackground: Finished");
        }
        return true;
    }
    
    public String getResult(){
        String r = result;
        result = null;
        return r;
    }
    

    The main thread waits for the result, and then retrieves the result with getResult():

    while(null == networktask.result){}
    Log.d("KMC.onCreate", networktask.getResult());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code I want to run, but the problem is $this->type
I have following code snippet that i use to compile class at the run
Let's say that I have the following code that's run in one thread of
I have the following code here that won't run on ARC since it combines
I have the following code, which is run every 10ms as part of a
I have the following code for a jquery mobile page. When I run it
I have the following sample code which doesn't seem to want to run. import
I have a line of code that gets the following error when run through
So I have created the following code snippet to run in a formula to
I have the following code, and it's giving me a parse error when run.

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.