• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Why should I use a new Socket to interrupt serverSocket.accept() ?

 
Ranch Hand
Posts: 75
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I want to shutdown server elegantly. In the code, serverSocket.accept() will be listening at the endpoint forever.

Approach 1, servletSocket.close() will case serverSocket.accept() to throw exception. Ugly, since my isGoingToShutdown flag is completely useless.

Approach 2, use "new Socket(serverSocket.getInetAddress(), serverSocket.getLocalPort()).close();" to interrupt serverSocket.accept(). Works. But why, why it works, and why should I do it in this way?
 
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just how the accept() method works. It blocks until a connection is made. If that's not good for you you should check out NIO and java.nio.channels.Selector. This will poll until there is actually activity; a connection request, data, etc. This polling can have a timeout so you won't have to wait indefinitely.
 
Leon Omk
Ranch Hand
Posts: 75
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm curious about interrupting accept(). If accept() is invoked, a ServerSocket is listening at the endpoint(local server address+port).

If we close this ServerSocket directly, accept() would be interrupted and an exception would be thrown, saying "socket closed".

But if we create a new socket(not serverSocket) on the same endpoint, then close it. The serverSocket.accept() will stop silently without any exception thrown! Why is there no exception?
 
Rob Spoor
Sheriff
Posts: 22650
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The new socket connection will cause accept() to not wait any longer, and return the other side of the connection. Since you abort the loop immediately and the server doesn't do anything with the socket there is no code that can throw an exception. Only if you call any methods on the closed socket will an exception be thrown.
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic