Results 1 to 6 of 6

Thread: Stop a multithreaded server

  1. #1
    Join Date
    Dec 2009
    Posts
    192

    Stop a multithreaded server

    Hello,
    I am currently testing a program which runs a server thread that listens on a port and launch the thread of services for customers. I have this code
    Code:
    while (! end) {
    				clsoc = sersoc.accept();
    Thread clthr = new Thread(new ServiceBiblio(
    						serverAddress, clsoc, g));
    clthr.home();
    end = stopThread;
    			}
    I found a stop function on internet and I used it in my code, here it is
    Code:
    Public synchronized void stop() {
            this.stopThread = true;
    }
    My problem is if I want to stop the server while it is listening, it does not work, it is not normal browsing loop. I do not see how to stop the server properly, I tried a violent serverSocket.close (), but this stops the main program too. If you have any more methods to do the same then do post back.

  2. #2
    Join Date
    Nov 2009
    Posts
    446

    Re: Stop a multithreaded server

    Hello,
    Is it another thing after the server is running your main program? Otherwise it is normal that your program stops after a close ()? What would you do after the termination of the server? What is the purpose of stopping the server in between, here the purpose is also necessary because no one needs to stop the server in between the process.

  3. #3
    Join Date
    Nov 2009
    Posts
    333

    Re: Stop a multithreaded server

    Hi,
    To stop the network connections should be used InterruptableChannel which can interrupt the blocking methods as "accept ()". For sockets so you must use the class ServerSocketChannelFor example
    Code:
    ServerSocketChannel sersocch = null;
    ServerSocket sersoc = null;
    Socket slsoc = null;
    		boolean en = false;
     
    		try {
    			SocketAddress address = new InetSocketAddress(9999);
    sersocch = ServerSocketChannel.open();
    sersoc = sersocch.socket();
    sersoc.bind(address);
    			
    			while (! en) {
    				try {
    					slsoc = sersoc.accept();
    					
    					/ / Process the slsoc
    					
    				} catch (ClosedByInterruptException plea of) {
    					en = true;
    				}
    			}
    		} catch (IOException plea of) {
    			/ / TODO Auto-generated catch block
    			The plea.printStackTrace();
    		} finally {
    			try {
    				if (sersoc! =null) {
    					sersoc.close();
    				} 
    			} catch (Exception plea of) {}
    			
    			try {
    				if (sersocch! =null) {
    					sersocch.close();
    				} 
    			} catch (Exception plea of) {}
    		}

  4. #4
    Join Date
    Dec 2009
    Posts
    192

    Re: Stop a multithreaded server

    Hello,
    Thanks for your replies, they helped me a lot , after stopping the server, the main program saves the data, configuration etc., and independently to stop the server, the program is used to make the administration on some data. Again thank you and good day. If you have more ideas about it, then please do post back, any explanation will be appreciated.

  5. #5
    Join Date
    Nov 2009
    Posts
    583

    Re: Stop a multithreaded server

    Hello,
    Check out this modified version of the code, may help you
    Code:
    ServerSocketChannel sersocch = null;
            ServerSocket sersoc = null;
            SocketChannel clsoc = null;
            boolean en = false;
    
            try (
                SocketAddress add = new InetSocketAddress (9999);
                sersocch = ServerSocketChannel.open ();
                sersoc sersocch.socket = ();
                sersoc.bind (add);
                
                while (en) (
                    try (
                        clsoc = sersocch. accept ();
                        
                        / / Process the clsoc
                        
                    ) Catch (ClosedByInterruptException plea of) (
                        en = true;
                    )
                )
            ) Catch ...

  6. #6
    Join Date
    Nov 2009
    Posts
    356

    Re: Stop a multithreaded server

    Hello
    I used your implementations using the channels in order to stop my thread blocked in accept (), and it works perfectly, or almost. In fact, now my program exits cleanly, but no more client can not connect to server. Since I use my server channels can not receive anything. You have an idea of where this might come? An other alternative will be great for me so, that I can try it out. Thank you for your help.

Similar Threads

  1. Is Age of Empires a multithreaded game
    By Dhruv in forum Video Games
    Replies: 3
    Last Post: 18-06-2009, 09:53 AM
  2. Replies: 0
    Last Post: 09-05-2009, 12:48 PM
  3. How to implement multithreaded parsing xml
    By EULALIA in forum Software Development
    Replies: 2
    Last Post: 08-05-2009, 12:07 PM
  4. How to stop Server management from starting
    By Faakhir in forum Networking & Security
    Replies: 2
    Last Post: 08-05-2009, 12:00 PM
  5. How can I stop the SMTP service from server exchange?
    By Camillus in forum Software Development
    Replies: 2
    Last Post: 17-02-2009, 08:25 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,751,780,685.35259 seconds with 16 queries