wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Closing a serversocket gracefully... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Closing a serversocket gracefully..." Watch "Closing a serversocket gracefully..." New topic
Author

Closing a serversocket gracefully...

Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
I have an console application and I need to make sure I have just one instance of the application running at any given point of time. So, I am using a serversocket connection
to check whether the application is running or not.
I am using a port 1111 and create a serversocket.
try
{
listenerSocket = new ServerSocket(11111);
}
catch(java.net.BindException nbe)
{
System.out.println("Already an instance running 1" );
System.exit(1);
}
First instancd runs. When you try to run another instance...since the port is occupied, it will exit.
But my question is, what is the application crashes. Will the socket gets closed??
If not, how do I close?
Help...Thanks
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Hi Bala -
If the original application instance crashes, you then rely on the operating system to recover the socket descriptor resource quickly. This can be an issue and you should test for it on your platform of choice. Certain (unpatched) versions of Solaris, for example, have a FIN_WAIT flag that can hang around for a very long time when a ServerScoket terminates abnormally.
If the socket is in the process of being reclaimed, new requests will be rejected, and you'll have the same behavior as if the port were busy doing somethign productive. Your best first resorts: thorough testing of the TCP/IP stack on your machine, and thorough checking of your server application's ability to recover from recoverable problems.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Bala Krishniah
Ranch Hand

Joined: Dec 14, 2000
Posts: 81
Thanks for the reply Michael.
I do not know anything about sockets. This my first little code I can say regarding Sockets. So please bear with me if my questions are silly.
I was thinking if the application crashes...and when you try to run the application again, it wont run since the port is still in use and was not released gracefully by the previous application.
In that case I have an option to run the application with the input parameter 'CLOSE'.
So if the application receives the parameter 'CLOSE', I check the socket object for 'null' if not then I call the close method on it.
ServerSocket listenerSocket;
:
:
if(args.length != 0)
try
{
if( args[0].equals("CLOSE"))
{
if( listenerSocket != null)
listenerSocket.close();
}
}
catch(Bind.....)
{
}
try
{
listenerSocket = new ServerSocket(11111);
}
catch(java.net.BindException nbe)
{
System.out.println("Already an instance running 1" );
System.exit(1);
}

Will that work. Will the next instance can catch hold of the socket instance.
Thanks for help!!
Laudney Ren
Ranch Hand

Joined: Jan 06, 2002
Posts: 111
I think it's right.
And that's a usual way of dealing with the problem.


" Veni, vidi, vici "<br />" I came, I saw, I conquered "
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Closing a serversocket gracefully...
 
Similar Threads
Preventing 2nd instance of Java application
ServerSocket and thread
Only one application in the Memory
Related to ServerSocket
close running instance of application when install new version