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 4231108
In Process

The Archive Base Latest Questions

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

I am confused as to how I can achieve something and how it should

  • 0

I am confused as to how I can achieve something and how it should be done as I believe there is more than one method;

I have an app and at the start it establishes a TCP connection to a server, then every now and again lets pretend you press a button and send a string to the server. Really the connection (I’ve used java.net.Socket) should run in a separate thread out of the main UI thread (which is where it currently is).

I have a public method so when the button is pressed, the method is called, and the data sent but again this runs from the main UI thread. Since the connection is constant throughout the life cycle of the app it should probably be an intent service, but can you have a call’able method within an intent service to send the data in the back ground over the back ground connection?

Or a service which connects the socket in the onStart() method and then within the service implement an asynctask? I’m lost!

  • 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-21T01:56:03+00:00Added an answer on May 21, 2026 at 1:56 am

    You can use synchronized to perform that, for example (and this is just a scratch of course, not handling any exception):

    public class SyncedTcpClient extends Thread{
        synchronized boolean new_data_available = false;
        String _data = null;
        public void run()
        {
            Socket s = new Socket("127.0.0.1", 3000);
    
            while(1)
            {
               synchronized(this)
                {
                    if(new_data_available)
                    {
                       // send data
                           new_data_available = false;
                    }
                    else
                    {
                       wait();
                    }
                }
            }
        }
        public void setData(String data)
        {
            _date = data;
            new_data_available = true;
        }
    }
    

    And in your activity (MyActivity.java) you add:

    OnClickListener myListener = new OnClickListener(){
        public void onClick(View v) {
            tcpThread.setData(data_to_send);
            tcpThread.notify();
        }
    };
    

    after the connection is established, the client will check if new data to send available, and if not, it will wait to notify. Also, you might find this question useful

    EDIT:
    Since it’s too much for a comment, I edited my answer.

    You have to start the tcpClient thread from somewhere, right? So do it from your activity. This way you’ll have the object of the thread (as we used it in myListener). regarding notify() – this is meaningless when we are not talking about threads (If I’m wrong, please someone correct me) and it’s purpose is to “free” threads that are on wait because of the same object to continue in execution, in this case is the client thread itself(synchronized(this)) so we tell it to continue, and just before that, we change the condition in the loop to be true (in the method setData(String)) so it will now send the data, and go into wait mode again.

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

Sidebar

Related Questions

confused - given a track uri, I can get one album but possibly more
I can't believe I'm still confused about this but, any way, lets finally nail
I'm pretty confused by this one and can't think of anything obvious that I
I will have four word list arrays. $array1 = array('red','blue','green');//it can have more elements
I am confused about something and need your help. There are some nice Product
I would like to achieve something like this one: Circular Content Carousel but I'm
I am confused about how to make a multipage app in one GWT project.
I am confused with how can i 'keep track of stack depth'. Actually, this
I'm a bit confused with this: can I use PHP variables in a pg
I am really confused about how I can do this. I need to get

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.