|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
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; } Code: Public synchronized void stop() { this.stopThread = true; } |
#2
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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
| |||
| |||
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. |
![]() |
|
Tags: client, java, multithreading, programming language, server, start server, stop server |
Thread Tools | Search this Thread |
|
![]() | ||||
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 |