Hi,
I'm writing a server-client program that communicates over TCP. When
a user connects, a new thread is created (yes, this is very bad, but
I'm learning).
The problem I'm having is when the user disconnects. If the user is
running the client program as an applet in a web browser, the server
does not remove their connection from the hashtable of clients when
they disconnect
Here is the code for the ServerThread class:
package server;
import java.io.*;
import java.net.*;
public class ServerThread extends Thread
{
// The main server thread
private Server server;
// The Socket that the client is connected to
private Socket socket;
String message;
// Constructor.
public ServerThread( Server server, Socket socket ) {
this.server = server;
this.socket = socket;
start();
}
public void run() {
try {
// Create a BufferedReader for communication
DataInputStream din = new DataInputStream(socket.getInputStream());
// Create an output stream for commands like ping
DataOutputStream dout = new
DataOutputStream( socket.getOutputStream() );
// Over and over, forever ...
while (true) {
// ... read the next message ...
message = din.readUTF();
System.out.println("Client "+socket+" sending command");
if (message != null)
{
// The input from the client is handled here, which results in a
message being sent back, and/or a message being sent to other clients
}
}
} catch( EOFException ie ) {
} catch( IOException ie ) {
ie.printStackTrace();
} finally {
System.out.println("Client disconnected");
// The client has disconnected, so have the server remove them from
the hashtable with this method
server.removeConnection( socket );
}
}
}
Is this something to do with Java Applets? I have tested this with
Firefox, and with the applet viewer, and the applet viewer closed the
connection fine.
Thanks in advance,
--James.
jamesgoode - 27 Nov 2007 20:37 GMT
> Hi,
>
[quoted text clipped - 68 lines]
>
> --James.
Just noticed, my comments are slightly wrong. There is no
BufferedReader ;-)
Gordon Beaton - 27 Nov 2007 20:58 GMT
> The problem I'm having is when the user disconnects. If the user is
> running the client program as an applet in a web browser, the server
> does not remove their connection from the hashtable of clients when
> they disconnect
How do they disconnect? You neglected to post that code. Are you sure
that they actually do disconnect?
> message = din.readUTF();
What should your code do if (or when) message is null?
/gordon
--
jamesgoode - 27 Nov 2007 22:01 GMT
> > The problem I'm having is when the user disconnects. If the user is
> > running the client program as an applet in a web browser, the server
[quoted text clipped - 11 lines]
>
> --
Here is the code for the removeConnection method:
void removeConnection( Socket s ) {
synchronized( outputStreams ) {
System.out.println( "Removing connection to "+s );
// Remove it from our hashtable/list
outputStreams.remove( s );
// Make sure it's closed
try {
s.close();
} catch( IOException ie ) {
System.out.println( "Error closing "+s );
ie.printStackTrace();
}
}
}
I know that the connections are destroyed, because it's shown on the
screen and while connected to the server, 0ponline returns the number
of connected users. If a message is null, it is just ignored - the
client has two threads, one to write data from a single-line input
box, and one to read data into a multiple-line box.
--James.
Andrew Thompson - 27 Nov 2007 23:58 GMT
...
> Is this something to do with Java Applets?
I woould say (guessing) that it is simply 'yet
another little oddity with applet/browser interaction'.
>..I have tested this with
> Firefox, and with the applet viewer, and the applet viewer closed the
> connection fine.
If that is the case. It sounds as though using JWS
to launch the applet is a much better option. JWS
uses the applet viewer to display applets.
e.g. <http://www.physci.org/jws/#jtest>
--
Andrew T.
PhySci.org
jamesgoode - 28 Nov 2007 13:13 GMT
> ...
>
[quoted text clipped - 15 lines]
> Andrew T.
> PhySci.org
Hi,
Thanks for your help, it works well, and is also much nicer than
having an applet inside the web page.
--James.