Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First make the <code>clientSocket</code> object local.</p> <p>Then for solving the looping problem try this:</p> <p>Call the function <code>starteMessageThreadRunner</code>, it will start the thread which will send the message to the server and as soon as the runnable will finish sending the data, it will again call the handler which in turn will call the thread again.</p> <pre><code> /** * start the thread and handler which will send the message */ public void starteMessageThreadRunner() { Message message = handler.obtainMessage(1, ""); handler.sendMessage(message); } /** * handler for calling the thread. */ Handler handler = new Handler() { public void handleMessage(Message msg) { // do something with the response received (if you want to) serverMessage.setText(msg.obj.toString()); //this line will call runnable again and will send the message to the server. handler.post(runnable); }; }; /** * A runnable object for calling the handler. */ private Runnable runnable = new Runnable() { @Override public void run() { // send the message to the server. try { Socket clientSocket= new Socket("192.168.0.103",2000); ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream()); oos.writeObject(et.getText().toString()); Message serverMessage= Message.obtain(); ObjectInputStream ois =new ObjectInputStream(clientSocket.getInputStream()); String strMessage = (String)ois.readObject(); Message message = handler.obtainMessage(1, strMessage ); oos.close(); ois.close(); handler.sendMessage(message); } catch(Exception e) { e.printStackTrace(); } } }; </code></pre> <p>And when you want to exit from this process call:</p> <pre><code>handler.removeCallbacks(runnable); </code></pre> <p>It will remove any pending posts of Runnable r that are in the message queue. </p> <p>Hope this helps!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload