Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 02-02-2010
Member
 
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.
Reply With Quote
  #2  
Old 02-02-2010
Member
 
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.
Reply With Quote
  #3  
Old 02-02-2010
Member
 
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) {}
		}
Reply With Quote
  #4  
Old 02-02-2010
Member
 
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.
Reply With Quote
  #5  
Old 02-02-2010
Member
 
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 ...
Reply With Quote
  #6  
Old 02-02-2010
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Stop a multithreaded server"
Thread Thread Starter Forum Replies Last Post
Is Age of Empires a multithreaded game Dhruv Video Games 3 18-06-2009 09:53 AM
Using multithreaded with Robocopy on Windows 7 and Windows Server 2008 R2 Jacek01 Tips & Tweaks 0 09-05-2009 12:48 PM
How to implement multithreaded parsing xml EULALIA Software Development 2 08-05-2009 12:07 PM
How to stop Server management from starting Faakhir Networking & Security 2 08-05-2009 12:00 PM
How can I stop the SMTP service from server exchange? Camillus Software Development 2 17-02-2009 08:25 PM


All times are GMT +5.5. The time now is 12:07 AM.