SOLVED: Problems with TCP server and its interface

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

SOLVED: Problems with TCP server and its interface

Postby puxdroid » Thu Nov 26, 2009 9:30 pm

Hello!

I'm developing an Android application that opens a TCP port in the mobile to handle requests.

I have created a button which when pressed runs a thread which creates a server socket like this:

serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();

But as accep() waits for a connexion and it stops the whole user interface, you can't do anything with it.

The server works ok, but the interface doesn't work.

Does anybody know how to make the main process not wait for the thread?

Thanks in advance
Last edited by puxdroid on Mon Nov 30, 2009 11:35 am, edited 1 time in total.
puxdroid
Developer
Developer
 
Posts: 49
Joined: Wed Nov 25, 2009 11:57 am

Top

Service process

Postby phperret » Fri Nov 27, 2009 2:12 pm

Hi puxdroid,

The thread must be run in his own service: you have to subclass Service class to implement your long running process.
--
Pierre
phperret
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Nov 27, 2009 2:09 pm
Location: france

Re: Service process

Postby puxdroid » Sat Nov 28, 2009 9:46 am

I don't understand the solution:

phperret wrote:The thread must be run in his own service: you have to subclass Service class to implement your long running process.


This is my main class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class test1 extends Activity {
  3.  
  4.  
  5.  
  6.         private ToggleButton mStartStopButton;
  7.  
  8.         private TextView mTexStat, mTexStat_, mLog;
  9.  
  10.  
  11.  
  12.         private LaunchServer launchServer;
  13.  
  14.  
  15.  
  16.         @Override
  17.  
  18.         public void onCreate(Bundle savedInstanceState) {
  19.  
  20.                 /**********************************/
  21.  
  22.                 Configurations.basic.port = 9865;
  23.  
  24.                 /**********************************/
  25.  
  26.  
  27.  
  28.                 super.onCreate(savedInstanceState);
  29.  
  30.                 setContentView(R.layout.webadmin);
  31.  
  32.  
  33.  
  34.                 // capture our View elements
  35.  
  36.                 mStartStopButton = (ToggleButton) findViewById(R.id.startstop);
  37.  
  38.                 mTexStat = (TextView) findViewById(R.id.status);
  39.  
  40.                 mTexStat_ = (TextView) findViewById(R.id.status_);
  41.  
  42.                 mLog = (TextView) findViewById(R.id.status_out);
  43.  
  44.  
  45.  
  46.                 // add a click listener to the button
  47.  
  48.                 mStartStopButton.setOnClickListener(new View.OnClickListener() {
  49.  
  50.                         public void onClick(View v) {
  51.  
  52.                                 if (mStartStopButton.isChecked()) {
  53.  
  54.                                         Toast.makeText(test1.this, "ON", Toast.LENGTH_SHORT)
  55.  
  56.                                                         .show();
  57.  
  58.                                         mTexStat.setText(R.string.text_starting);
  59.  
  60.  
  61.  
  62.                                         LaunchServer ls = new LaunchServer();
  63.  
  64.                                         Thread thread = new Thread(ls);
  65.  
  66.                                         thread.setDaemon(true);
  67.  
  68.                                         thread.run();
  69.  
  70.  
  71.  
  72.                                         mTexStat.setText(R.string.text_running);
  73.  
  74.                                 } else {
  75.  
  76.                                         Toast.makeText(test1.this, "OFF", Toast.LENGTH_SHORT)
  77.  
  78.                                                         .show();
  79.  
  80.                                         mTexStat.setText(R.string.text_stopping);
  81.  
  82.                                         stopWebServer();
  83.  
  84.                                 }
  85.  
  86.  
  87.  
  88.                         }
  89.  
  90.                 });
  91.  
  92.  
  93.  
  94.                 mTexStat.setText(R.string.text_stopped);
  95.  
  96.                 mTexStat_.setText(R.string.text_status);
  97.  
  98.  
  99.  
  100.         }
  101.  
  102.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



LaunchServer is the classs wich have the server socket:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class LaunchServer implements Runnable {
  3.  
  4.  
  5.  
  6.         private ServerSocket serverSocket;
  7.  
  8.  
  9.  
  10.         public LaunchServer() {
  11.  
  12.         }
  13.  
  14.  
  15.  
  16.         public void run() {
  17.  
  18.  
  19.  
  20.                 try {
  21.  
  22.  
  23.  
  24.                         serverSocket = new ServerSocket(Configurations.basic.port);
  25.  
  26.                        
  27.  
  28.                         // server infinite loop
  29.  
  30.                         while (true) {
  31.  
  32.                                 Socket socket = serverSocket.accept();
  33.  
  34.  
  35.  
  36.                                 try {
  37.  
  38.                                         RequestHandler request = new RequestHandler(socket);
  39.  
  40.                                         // Create a new thread to process the request.
  41.  
  42.                                         Thread thread = new Thread(request);
  43.  
  44.  
  45.  
  46.                                         // Start the thread.
  47.  
  48.                                         thread.start();
  49.  
  50.                                 } catch (Exception e) {
  51.  
  52.                                        
  53.  
  54.                                 }
  55.  
  56.                         }
  57.  
  58.                 } catch (IOException e) {
  59.  
  60.                        
  61.  
  62.                 }
  63.  
  64.  
  65.  
  66.         }
  67.  
  68.  
  69.  
  70. }
  71.  
  72.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


But I don't know how to make the main process not wait for the thread.

Does anybody know how to solve this problem?
Last edited by puxdroid on Mon Jan 18, 2010 3:08 pm, edited 1 time in total.
puxdroid
Developer
Developer
 
Posts: 49
Joined: Wed Nov 25, 2009 11:57 am

Postby virtual » Mon Nov 30, 2009 6:59 am

class TT implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub

}
}

And for creating thread....

Thread t = new Thread(new TT());
t.start(); // not t.run()
Regards,
Virtual
virtual
Developer
Developer
 
Posts: 26
Joined: Tue Mar 24, 2009 11:20 am

Postby puxdroid » Mon Nov 30, 2009 11:29 am

You are right!
Now it works! A lot of thanks!
puxdroid
Developer
Developer
 
Posts: 49
Joined: Wed Nov 25, 2009 11:57 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 4 guests