I have trouble with start and stop a socket server, to start the server I
put the webServerOn on true and incocate the superclass start() method, it
run ok, but when I need to stop I use the stopWebServer() method and an
exception occured :
java.net.SocketException: socket closed
at java.net.PlainSocketImpl.socketAccept(Native Method)
Webserver stopped
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:406)
at java.net.ServerSocket.implAccept(ServerSocket.java:240)
at java.net.ServerSocket.accept(ServerSocket.java:224)
at WebServer.run(WebServer.java, Compiled Code)
and I can't restarted the serversocket
here is a simplification of the code
public class WebServer extends Thread
{
public void stopWebServer()
{
boolean webServerOn;
if(webServerOn)
{ webServerOn = false;
try
{serverSocket.close();}
catch( IOException ioe )
{;}
serverSocket = null;
}
}
public synchronized void run()
{
try
{serverSocket = new ServerSocket(port);
while (webServerOn)
{socket = serverSocket.accept();
socket.setSoTimeout( timeout );
socket.setTcpNoDelay(true);
System.out.println("From " +socket.getInetAddress().getHostAddress());
socket.close();
}
}
catch( Exception e )
{;}
}
}
Please help, me
Cheers,
Thread.interrupt();
in stopWebServer()
This will stop ServerSocket.accept();
before ServerSocket.close();
just because
Even if you change the flag
webServerOn = false;
the thread runs and Socket acception is working...
and when You close the ServerSocket...
It throws SocketException...
so when you interrupt the acception before close the ServerSocket.
No Problem occurs...
(Actually InterruptedException thrown.)...
riceman
In java lots of times you'll see in the doc that once something has been closed
it cannot be reopened. This is probably true of sockets. However, this really
isn't a problem. After closing the ServerSocket, all you have to do is make a
new ServerSocket if you want to continue running.
Don
<eric.g...@evc.net> wrote in message news:83p8o9$idf$1...@jaydee.iway.fr...