Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

socket.accept() + socket.close()  RSS feed

 
Luiz Reginaldo Curado
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I want to close my server socket, but my server thread is blocked in the accept() call. In my implementation I call close() and a Socket Exception is throwed. Is this the correct way to stop my server?

public void startServer() {
try {
...
socket.accept() ;
...
} catch( SocketException e ) {
e.printStackTrace() ;
}
}

public void stopServer() {
...
socket.close() ;
...
}
 
chris bajada
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is the correct way to stop the server. The SocketException is thrown, as the socket can no longer listen for incoming client connections.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!