Results 1 to 6 of 6
  1. #1
    zardos is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default How to close this thread? (Socket listener)

    Hello i have this thread and whould be able to close by maby setting a variable to false.

    But dont realy now how to make it stop listening?

    how can i interup .accept() ? if i do this and insert a flag in the while loop?

    //Thanks!

    Java Code:
    public class ServerThread implements Runnable {
    
    	        public void run() {
    	            try {
    	                if (SERVERIP != null) {
    	                    serverSocket = new ServerSocket(SERVERPORT);
    	                    while (true) {
    
    	                        // listen for incoming clients
    	                        Socket client = serverSocket.accept();
    
    	                        try {
    	                            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    
    	                            DataOutputStream output = new DataOutputStream(client.getOutputStream());
    	                            String line = null;
    	                        
    	                            String data;
    	                            output.writeBytes(data);
    	                            output.flush();
    	                            output.close();
    	                           
    	                            while ((line = in.readLine()) != null) {
    	                                Log.d("ServerActivity", line);
    	                            }
    	                            
    	       
    	                        } catch (Exception e) {
    	                            e.printStackTrace();
    	                        }
    	                        client.close();
    	                    }
    	                } else {
    	                
    	                }
    	            } catch (Exception e) {
    	           
    	                e.printStackTrace();
    	            }
    	        }
    	    }

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    How you described seems like the best solution to me, just use a bool variable instead of true in the while conditional and maybe have it set to false if it reads a certain line from the input stream or however you want to do that.

  3. #3
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    @Shoss That doesn't work exactly here, the accept method is blocking. What I would do is set a short timeout:

    Java Code:
    serverSocket.setSoTimeout(500) //timeout for the accept method only, in ms
    When you call accept, it will throw an exception at the timeout. Basically, it would timeout, and depending on your boolean, retry.

    Java Code:
    serverSocket = new ServerSocket(SERVERPORT);
    serverSocket.setSoTimeout(500);
    while(shouldAccept && server.isClosed()){ //shouldAccept is the boolean, maybe it is !isBound instead of isClosed
        try{
             serverSocket.accept();
        }
        catch(SocketTimeoutException e){
            System.out.println("Retrying...");
        }
    }
    //do stuff
    Or, you could launch a new Thread, and interrupt it when you need to (Not sure about this one).
    There might (should) be a better solution, but that is what I have had to resort to.
    Last edited by rp181; 03-02-2011 at 10:42 PM.

  4. #4
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default

    Ah, true. My way would only work if he connected first and fed the server a quit command.

  5. #5
    zardos is offline Member
    Join Date
    Jan 2011
    Posts
    8
    Rep Power
    0

    Default

    Whould it be bad if i just called ServerThread.interrupt() on the whole thing?

    I dont care and no info till we lost, but will a plane :

    fst = new Thread(new ServerThread());
    fst.start();

    and then

    fst.interrupt()

    cause me any problems, all i want is for the lisening to stop?

  6. #6
    rp181 is offline Member
    Join Date
    Mar 2009
    Posts
    70
    Rep Power
    0

    Default

    Try it. However, in the API, there is no throws Interruptable exception, so don't know if it would work.

Similar Threads

  1. Replies: 1
    Last Post: 07-02-2010, 02:18 PM
  2. Replies: 13
    Last Post: 04-09-2010, 12:15 PM
  3. best way to signal a thread that's blocking on a socket receive
    By danieru in forum Threads and Synchronization
    Replies: 5
    Last Post: 09-08-2009, 05:47 PM
  4. append response to the request from Socket and write to another socket
    By vaibhav_singh_vs@yahoo.co in forum Networking
    Replies: 3
    Last Post: 04-17-2009, 08:02 PM
  5. freezing when use socket.accept() inside of a thread
    By tamayo in forum Advanced Java
    Replies: 0
    Last Post: 07-24-2007, 12:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •