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.
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.
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) {}
}
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.
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 ...
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.