PDA

Click to See Complete Forum and Search --> : Applet / ServerSocket


RoyceWindsor1
Feb 21st, 2002, 12:55 PM
I have a signed applet setup to "listen" for a command on a certain port. The code works fine until the user hits the "refresh" button in the browser (testing in IE6). Here's how the code is set up:


public class CTIListener extends java.applet.Applet {
public void init() {
}

public void start() {
if (sockListen == null) listen();
}

public void stop() {
if (sockListen != null) {
try {
sockListen.close();
sockListen = null;
} catch(IOException e) {
}
}
}

public void dispose() {
if (sockListen != null) {
try {
sockListen.close();
sockListen = null;
} catch(IOException e) {
}
}
}

public void listen() {
// Opens server socket and calls Accept method
}
}



The initial call to listen() comes from the init() method of another applet on the same page.

When the brower is refreshed, I get an error saying "Address already in use." What could be happening?

John

Mrs Kensington
Feb 22nd, 2002, 02:30 AM
err wheres sockListen declared, what type is it?

This is the order that methods are called with an applet

(This is only tru in IE 6 I think netscape behaves differently)

When an applet is first loaded up...
1. init()
2. start()

When a page is refreshed...
1. stop()
2. init()
3. start()

When an applet exits...
1. stop()

But i can't see what the problem is as stop should be called thus closing the connection and setting it to null. So it should be able to be reopened.

Sorry!

Mrs Kensington
Feb 22nd, 2002, 02:38 AM
Actually one thing i just thought of.

When i was doing the testing to get the above method orders I was using JOptionPanes to show the method names.
When i refreshed the page init() was shown as first, then start and then stop. This originally confused me (alot).
Turns out that when the init() JOptionPane appeared. If i moved it away the stop one was underneath.

I'm assuming that Internet Explorer starts a new Thread when you hit the refresh button. So init() was being called before stop() had finished and thus the socket was closed.

That shouldn't matter though as your sockets initialised in start() not init(). But if it manages to get to start before stop has finished then it wouldn't be able to open the socket and would throw an exception.

Hope you understand my reasoning there!

I haven't given a (and dont know a) solution but hopefully I've pointed you in the right direction of the cause.

But then again i could be completely wrong. It is early!

Mrs K

RoyceWindsor1
Feb 22nd, 2002, 07:36 AM
Originally posted by Mrs Kensington
Actually one thing i just thought of.

That shouldn't matter though as your sockets initialised in start() not init(). But if it manages to get to start before stop has finished then it wouldn't be able to open the socket and would throw an exception.



Thanks for the response. The problem was basically what you describe. The "stop" on the listener applet had not completed before the "init" was running on the second applet. I opted instead to always start listening in the listener applet (s new thread from the start method) and that seemed to cure the problem.

John