Home arrow static arrow Java Programming [Archive] - Shutting down ServerSocket
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Shutting down ServerSocket
This topic has 4 replies on 1 page.

Posts:87
Registered: 8/5/02
Shutting down ServerSocket  
Jul 15, 2004 7:00 AM



 
I have a client that connects to a server. Each client creates and runs its own server. When the client disconnects, I want to shutdown the server. However, when I do this, the client's server is in the serverSocket.accept(); method, so when I close the socket, I get the error on the serverSocket.accept(); line saying that the socket is closed.

Is this OK? Or is there a better way to do this? Since the accept method blocks until it receives a connection, I don't see how to get around this...

Thanks!
 

Posts:65
Registered: 4/9/04
Re: Shutting down ServerSocket  
Jul 15, 2004 7:10 AM (reply 1 of 4)



 
Thre's something i don't understand. A client tries to connect and the server "listen" to that client until it disconnects??? or a server listens on a port and every time somebody connects to the port, the server launches a Thread???

Sorry for my english
 

Posts:87
Registered: 8/5/02
Re: Shutting down ServerSocket  
Jul 15, 2004 7:15 AM (reply 2 of 4)



 
I have a Server and a Client. Ignore the Server for now, just assume the Client connects to the Server.

When the Client connects, the Client itself wants to start its OWN server (so it can accept direct connections from other Clients). So, the Client creates it's own Server to listen for connections. When the Client closes the application, I want to shut down the server (the client server that is). This is where the error occurs.

I hope this is more clear!

p.s. - I think we can think of this a bit more easily as just a Server. Let's say there is an option to "shut down the server". When that is selected, the ServerSocket will be closed. When this happens, an error is generated because the ServerSocket is blocked on serverSocket.accept(). I don't see any way around this error and I want to know if it is just because I am not implementing this correctly.
 

Posts:319
Registered: 11/8/00
Re: Shutting down ServerSocket  
Jul 15, 2004 7:45 AM (reply 3 of 4)



 
Close the ServerSocket

http://java.sun.com/j2se/1.4.2/docs/api/java/net/ServerSocket.html#close()

This should close the socket, anything connected will throw a SocketException. so handle this in your clients, the other option would be to only close the socken once all clients had disconnected, not sure how to do this with a standard ServerSocket but you can do it using java.nio classes.

hope this helps
 

Posts:87
Registered: 8/5/02
Re: Shutting down ServerSocket  
Jul 15, 2004 7:58 AM (reply 4 of 4)



 
Right, I just wanted to verify that it would throw an exception. It is being caught, but I didn't quite know what to do with it. I didn't know if I should print this message or just suppress it...just in case it could be an error from something else.
 
This topic has 4 replies on 1 page.