Home > Archive > Java Help > November 2007 > Very horrible applet problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Very horrible applet problem
|
|
| jamesgoode 2007-11-27, 7:24 pm |
| 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 2007-11-27, 7:24 pm |
| On Nov 27, 8:36 pm, jamesgoode <ja...@jgoode.co.uk> wrote:
> 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.
Just noticed, my comments are slightly wrong. There is no
BufferedReader ;-)
| |
| Gordon Beaton 2007-11-27, 7:24 pm |
| On Tue, 27 Nov 2007 12:36:05 -0800 (PST), jamesgoode wrote:
> 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 2007-11-27, 7:24 pm |
| On Nov 27, 8:58 pm, Gordon Beaton <n....@for.email> wrote:
> On Tue, 27 Nov 2007 12:36:05 -0800 (PST), jamesgoode wrote:
>
> How do they disconnect? You neglected to post that code. Are you sure
> that they actually do disconnect?
>
>
> What should your code do if (or when) message is null?
>
> /gordon
>
> --
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 2007-11-27, 7:24 pm |
| On Nov 28, 6:36 am, jamesgoode <ja...@jgoode.co.uk> wrote:
....
> 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 2007-11-28, 8:12 am |
| On 27 Nov, 23:58, Andrew Thompson <andrewtho...@gmail.com> wrote:
> On Nov 28, 6:36 am, jamesgoode <ja...@jgoode.co.uk> wrote:
> ...
>
>
> I woould say (guessing) that it is simply 'yet
> another little oddity with applet/browser interaction'.
>
>
> 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
Hi,
Thanks for your help, it works well, and is also much nicer than
having an applet inside the web page.
--James.
|
|
|
|
|