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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:05:43+00:00 2026-06-14T07:05:43+00:00

I’ve created a remote service that taking care for all client-server communication. I use

  • 0

I’ve created a remote service that taking care for all client-server communication.
I use service because there are few separated applications that will use the same communication socket and there is no other way to “share” socket between applications (as far as i know).

The service works great, can start a socket connection and send int/String through it, but i can’t use it as input like readString().

I think the problem occurs because the activity never wait for reply from the service.
I tested it while returning custom strings in every part of my readString method on my service.

and for the code…

ConnectionRemoteService:

public class ConnectionRemoteService extends Service {

private String deviceID;
private ConnectionThread ct;

@Override
public void onCreate() {
    super.onCreate();
    //Toast.makeText(this, "Service On.", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {
    //Toast.makeText(this, "Service Off.", Toast.LENGTH_LONG).show();
    if(ct != null)
        ct.close();
}

@Override
public IBinder onBind(Intent intent) {
    return myRemoteServiceStub;
}   

private ConnectionInterface.Stub myRemoteServiceStub = new ConnectionInterface.Stub() {
    public void startConnection(){
        WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        deviceID = wm.getConnectionInfo().getMacAddress();
        ct = new ConnectionThread(deviceID);
        ct.start();
    }

    public void closeConnection(){
        if(ct != null)
            ct.close();
    }

    public void writeInt(int i) throws RemoteException {
        if(ct != null)
            ct.writeInt(i);
    }

    public int readInt() throws RemoteException {
        if(ct != null)
            return ct.readInt();
        return 0;
    }

    public void writeString(String st) throws RemoteException {
        if(ct != null)
            ct.writeString(st);
    }

    public String readString() throws RemoteException {
        if(ct != null)
            return ct.readString();
        return null;
    }

    public String deviceID() throws RemoteException {
        return deviceID;
    }

    public boolean isConnected() throws RemoteException {
        return ct.isConnected();
    }

};

}

explanation:

as you can see, i only start an “empty” service and wait for application to bind with it.
after the bind, i create ConnectionThread that will take care for the socket etc…
all methods calls the thread methods for input \ output through the socket.

ConnectionThread:

public class ConnectionThread extends Thread {

private static final int SERVERPORT = 7777;
private static final String SERVERADDRESS = "192.168.1.106";

private String deviceID;
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
private ObjectInputStream inObj;
private ObjectOutputStream outObj;
private boolean isConnected = false;

PingPongThread ppt;

public ConnectionThread(String deviceID) {
    super();
    this.deviceID = deviceID;
}

@Override
public void run() {
    super.run();
    open();

}

void open(){
    try{
        socket = new Socket(SERVERADDRESS,SERVERPORT);
        out = new DataOutputStream(socket.getOutputStream());
        out.flush();
        in = new DataInputStream(socket.getInputStream());
        outObj = new ObjectOutputStream(out);
        outObj.flush();
        inObj = new ObjectInputStream(in);
        out.writeUTF(deviceID);
        isConnected = true;
        ppt = new PingPongThread(SERVERADDRESS, SERVERPORT);
        ppt.start();
    }
    catch(Exception  e){
        isConnected = false;
        System.err.println(e.getMessage());
    }
}

public void close(){
    try {
        if(ppt!=null){
            ppt.stopThread();
            ppt.notify();
        }
        if(in!=null)
            in.close();
        if(out!=null)
            out.close();
        if(socket!=null)
            socket.close();
    } 
    catch(Exception  e){
        System.err.println(e.getMessage());
    }
    isConnected = false;
    socket=null;

}

public void writeInt(int i){
    try {
        out.writeInt(i);
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public int readInt(){
    try {

        int i = in.readInt();
        return i;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 0;
}

public void writeString(String st){
    try {
        out.writeUTF(st);
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public String readString(){
    String st = "";
    try {
        st = in.readUTF();
        return st;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return st;
}

public boolean isConnected(){
    return isConnected;
}

}

explanation:

in my thread, i create the socket and initialize all in/out objects to use later on.
(ignore the “PingPongThread”, it’s just a simple thread to check connection. it uses different port, so it can’t be the problem…)
all other methods are very simple, just using the in/out objects…

and for the main activity:

public class MainLauncherWindow extends Activity {
private ConnectionInterface myRemoteService;
private boolean isServiceBinded = false;
private OnClickListener onclicklistener;

final ServiceConnection conn = new ServiceConnection() {
    public void onServiceConnected(ComponentName name, IBinder service) {
        myRemoteService = ConnectionInterface.Stub.asInterface(service);
    }
    public void onServiceDisconnected(ComponentName name) {
        myRemoteService = null;
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_launcher_window);

    final Button connectButton = (Button)findViewById(R.id.Connect);
    final Button disconnectButton = (Button)findViewById(R.id.Disconnect);


    startService(new Intent(getApplicationContext(), ConnectionRemoteService.class));
    isServiceBinded = bindService(new Intent("com.mainlauncher.ConnectionRemoteService"),conn,Context.BIND_AUTO_CREATE);

    //Connect button
    onclicklistener = new OnClickListener(){
        public void onClick(View v) {
            try {
                if(isServiceBinded){
                    myRemoteService.startConnection();
                    connectButton.setEnabled(false);
                    disconnectButton.setEnabled(true);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    };
    connectButton.setOnClickListener(onclicklistener);

    //Disconnect button
    onclicklistener = new OnClickListener(){
        public void onClick(View v) {
            connectButton.setEnabled(true);
            disconnectButton.setEnabled(false);
            try {
                if(isServiceBinded)
                    myRemoteService.closeConnection();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };
    disconnectButton.setOnClickListener(onclicklistener);


    //read test button
    final Button bt1 = (Button)findViewById(R.id.bt1);

    onclicklistener = new OnClickListener(){
        public void onClick(View v) {
            try {
                if(isServiceBinded){
                    myRemoteService.writeString("Testing");
                    Toast.makeText(v.getContext(), myRemoteService.readString(), Toast.LENGTH_LONG).show();
                }
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    };
    bt1.setOnClickListener(onclicklistener);        
}




@Override
public void onBackPressed() {
    super.onBackPressed();
    if(isServiceBinded){
        unbindService(conn);
        stopService(new Intent(getApplicationContext(), ConnectionRemoteService.class));
        isServiceBinded = false;
    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if(isServiceBinded){
        unbindService(conn);
        stopService(new Intent(getApplicationContext(), ConnectionRemoteService.class));
        isServiceBinded = false;
    }
}



}

in my main activity i created buttons for connect \ disconnect and test button.
the test button sends “Testing” string to the server side.
the server works fine, gets the “Testing” String and returns other string to the client.
but the “Toast” msg is blank always.

  • i tested the server side without the service and it works fine, so no worries there.
  • i had a test with the ConnectionThread, returning test string from it’s readString method and it worked well, means the thread returns an answer through the service to the client side (all the chain works well).

the only thing i have in mind now is that the activity never waits for a string back from the service and that’s what cause the problems.

any ideas?

thanks,
Lioz.

  • 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-14T07:05:44+00:00Added an answer on June 14, 2026 at 7:05 am

    Try using AsyncTask instead of thread or wrap your thread in an AsyncTask(if it is possible i didnt tried it)

    by doing that you can make your app wait for your result like this:

     class YourClass  extends AsyncTask <Bla,Object,Bla>{
    
     doInBackGround(Bla blah){
      //do the stuff here
      return result;
     }
    
    
     onPostCalculate(Object result){
    
      // use the result.
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

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.