aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes How do I close a bound socket? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "How do I close a bound socket?" Watch "How do I close a bound socket?" New topic
Author

How do I close a bound socket?

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
I have an applet that connects to a servlet that kicks off a ServerSocket. Works fine. But when I close the html page in the browser and reopen it, I get a java.net.BindException, Address already in use: JVM_Bind - I have to restart the Tomcat server to make it work properly again. I don't want that. I have isolated the problem to just this part of the code.

I'm looking for a solution. I've been trying to find a way to check to see if it's bound, but get the complaint that it might not be initialized (remember it's designed to be initialized when the browser page is opened; can't initialize is the problem if it's already been and I don't know how to check to see if it's bound). Can't figure out how to either destroy it directly to be sure or check if it needs to be; without running into the might not be initialized error. (So it doesn't compile.)

Even when the method() that starts this server initializes I guess the port must still be bound somehow, or something. I still get the BindException error.


Correlation does not prove causality.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

You need to call close() on the ServerSocket before you lose the reference to the object. If you really need to open it in the request method, then use a "finally" block around the whole body of the request method and make sure it gets closed in your finally block.


[Jess in Action][AskingGoodQuestions]
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
Ernest Friedman-Hill wrote:You need to call close() on the ServerSocket before you lose the reference to the object. If you really need to open it in the request method, then use a "finally" block around the whole body of the request method and make sure it gets closed in your finally block.


Not sure how to get this to work. I tried putting the finally in my "request method" to close the input stream. That's the same thing as the javascript unLoad -> calls method that closes the input stream that I was doing before. No effect.

I also tried adding a finally around the offending code on the server side; but that just gives me the "serverSocket might not have been initialized" compiler error again.


Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
Ernest Friedman-Hill wrote:You need to call close() on the ServerSocket before you lose the reference to the object. If you really need to open it in the request method, then use a "finally" block around the whole body of the request method and make sure it gets closed in your finally block.


In the requesting method; I've been to the Java API several times, thinking there must be a way to URL.close() or something, but haven't found it.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Just make sure the variable will always be initialized, like this:

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
Ernest Friedman-Hill wrote:Just make sure the variable will always be initialized, like this:

....


OK, that got me past the might not be initialization problem. Now the second time in, I get:

You can see a couple of posts up, in the code for the requesting method - where this is being thrown. This is for doing nothing but bringing up the webpage the second time.

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
the servlet code.

Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Something's throwing an exception on the server side at runtime, and this will give you an idea of what's going on. You'll have to look at the server logs -- i.e., if this is tomcat, look in logs/catalina.out .

I am having a hard time understanding what you're trying to do. Why do you need to open a ServerSocket in the servlet, anyway? Where is the code that opens an ObjectOutputStream to send something to the ServerSocket?
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
Ernest Friedman-Hill wrote:Something's throwing an exception on the server side at runtime, and this will give you an idea of what's going on. You'll have to look at the server logs -- i.e., if this is tomcat, look in logs/catalina.out .

I am having a hard time understanding what you're trying to do. Why do you need to open a ServerSocket in the servlet, anyway? Where is the code that opens an ObjectOutputStream to send something to the ServerSocket?


I'm checking the logs, etc. The problem may lie in one of the wayward attempts I did today at trying to fix the first problem. While I'm checking:

It's my attempt at a push implementation. I have stuff going on elsewhere, in other programs, even other systems. When any system wants to report something, it sends a message to the simple server ServerSocket, which writes the message to the waiting URLConnection back to the applet, which sends it into the browser.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
BTW: I had this setup working once before, using Multicasting. But that only worked in my LAN with security set to use a policy. I'm implementing this with URLConnection, and even though that only allows one "monitor", it can be used from any browser without installing any software. So far I've had one successful test, using a little command line reader that just chucks whatever I type into the command line up into the browser. So - proof of concept done.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
I can't find a catalina.out log. But in the catalina.date.etc log I found what does not appear to be the problem we've been discussing, since it seems to happen when I shut down Tomcat (although it does look like something that needs to be fixed, and may be related?):
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
It's part of a system that's scheduled to be open-sourced by the end of the month. Any help would be appreciated.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
In the localhost log: It's the serverSocket.close() that I added in the finally clause yesterday in an attempt to assure that it's closed.

Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
Maybe the problem is that this isn't what servlets are designed for. The intent and design is to get a servlet loaded and then assume it's needed to service a large number of hits from different users. I'm sure I read somewhere that it does just that - loads and persists.

I think I really need to know how to check everything before doing anything. That's been tricky. The Java API hasn't shown me the ways. I suppose I'll have to spend a day or two doing trial an error.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
I was considering a reimplementation today to try using a program object rather than a servlet. Then I ran across this: URLConnection sample program in Java

The URL and URLConnection classes are good enough for simple programs that want to connect to HTTP servers to fetch content. For more complex applications, you'll probably find that you are better off studying the specification of the HTTP protocol and implementing your own wrappers.


On that note, I think I'll just be satisfied for the moment that I have something that works (even though you have to restart Tomcat every time you exit the webpage, before re-entering). I'll hope a network guru decides to contribute to the open-source project. If not, I'll see if I can get back to this later. Fact is, this interface stuff is just extra goodies; not actually part of the main body of code being offered by my open source project. I have to get my attention back on the main stuff to hit my target date for putting the code up.
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 367
OK, it's fixed now. What a kluge! I'll just describe generally. What I was trying to do wouldn't work. OK, I've learned that. I realized however, that I was only using the doPost(), leaving the doGet() open for clean up operations. I confirmed that I was in fact doing the doGet() properly by getting a response, and then put the code in the applet's stop() method. No luck. I did eventually get a change in the error message. That, I think should work, but I don't know why it's not. So ---- I accepted the fact that I would, in some circumstances, get errors, and concentrated on error handling. I had to follow the errors all the way back to the applet, where it was causing the process loop to shut down. So, in handling the error there, I sent a message back to the web-page to reload the page. I also inserted a send message to the socket in the start() method, to confirm that the connection is established, which should put a message "Connection established" in the GUI, but doesn't. I don't know why. Maybe I'll look at that tomorrow. But nonetheless. I can exit the web page and come back, repeat, repeat, repeat, and everything continues to work as it should.
 
wood burning stoves
 
subject: How do I close a bound socket?