Hello all,
i'd like to prematurely thank anyone who responds to the message.
I am running a Jdk1.3,
servlet 2.2, Jrun 3.0
I have a problem in detecting the socket connections interrupted between
applet & servlet. Assume the client(applet) & servlet are communicating through socket. when the internet connections is lost b'n applet & servlet , in the applet i get notified but not in the servlet.
I have a code example below, I implement the watcherHost on both the applet & servlet. In the applet when connection is
interrupted it throws IOException : socket read failed.
I even implemented the non blocking i/o for every 10 seconds, so that when connection is lost it can't read any more & throw
IOexception this is not happening on servlet side ?
my question
when the connection is lost why IOException is not thrown on server(servlet)side , since it cant read any more on that socket? any idea any adivce.
////////************watcher.java**************//////////
public class Watcher extends
Thread {
Socket socket;
WatcherHost host;// who is using the CobroCommunicator
boolean stopping = false;
boolean discByPeer = true;
public Watcher(WatcherHost host,Socket socket) {
this.host = host;
this.socket = socket;
setPriority(NORM_PRIORITY - 1);
}
public boolean disconnectByPeer() { return discByPeer; }
public void setHost(WatcherHost h) { host = h; }
public WatcherHost getHost() { return host; }
public void disconnect() {
// Let the latest message go.
//try { Thread.sleep(1000L); }
//catch (InterruptedException e) {}
stopping = true;
discByPeer = false;
synchronized (socket) {
try { socket.close(); }
catch (IOException e) {}
}
}
public void sendMsg(BaseMessage msg) {
host.showStatus("SEND: [" + String.valueOf(msg.type) + "]");
Talker sender = new Talker(msg);
sender.start();
}
public void run() {
int
test = 0;
boolean isException = false;
while (!stopping) {
try {
socket.setSoTimeout(10000);
// Read the first line sent by the client
ObjectInputStream in = new ObjectInputStream(
new BufferedInputStream(socket.getInputStream()));
BaseMessage msg = (BaseMessage)(in.readObject());
//if(msg==null) stopping = true;
host.showStatus("RECV: [" + String.valueOf(msg.type) + "]");
//host.showStatus("TimeOUT :");
host.dispatch(msg, this);
}
catch (NullPointerException e) {
stopping = true;
isException = true;
}
catch (ClassNotFoundException e) {
stopping = true;
isException = true;
}
catch(InterruptedIOException e){
System.out.println("watcher.run().InterruptedIOException: " + e.getMessage());
host.showStatus("TimeOUT :");
}
catch (IOException e) {
System.out.println("Watcher.run() throws IOException: " + e.getMessage());
if (!stopping) {
isException = true;
if (test < 3) {
++ test;
try { Thread.sleep(1000L); }
catch (InterruptedException ie) {}
}
else
stopping = true;
}
// else {
// stopped by disconnect(). Or
// after testing 3 times.
// }
}
}// end while
// If is isException, close the socket.
// Otherwise, the socket already close by disconnect()
if (isException) {
synchronized (socket) {
try { socket.close(); }
catch (IOException e) {}
}
}
// Inform host before going, anyway
host.disconnected(this);
}
class Talker extends Thread {
BaseMessage msg;
public Talker(BaseMessage p_m) {
msg = p_m;
}
// Send message
public void run () {
if (socket == null) return;
synchronized(socket) {
try {
ObjectOutputStream os = new ObjectOutputStream (
new BufferedOutputStream(socket.getOutputStream()));
os.writeObject ( msg ) ;
os.flush();
}
catch (IOException e) {
System.out.println("Watcher.Talker.run() throws IOException: " + e.getMessage());
}
}
}
}
}
//////////////***********watcherhost.java *********////////////
public interface WatcherHost {
public void dispatch(BaseMessage msg, Watcher watcher);
public void disconnected(Watcher watcher);
public void showStatus(
String msg);
}