aspose file tools*
The moose likes Applets and the fly likes Server pushing events to Applet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Server pushing events to Applet" Watch "Server pushing events to Applet" New topic
Author

Server pushing events to Applet

Victor Ho
Ranch Hand

Joined: Sep 05, 2003
Posts: 74
Hi,

How does one typically solve the problem of having a server pushing events to an applet through HTTP? I am trying to avoid making the applet poll the server for events.

I am wondering if it is possible for an applet to open a socket and listen on a port after it has been downloaded onto the client machine. My guess is NO, cuz otherwise a malicious applet could just use up the client's machine network resources.

In the case of stock streamers, I suppose the server must have some way to push events to the applet through HTTP (to work across firewalls). Any idea how this is done in a scalable fashion?

Thanks
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

Originally posted by Victor Ho:

I am wondering if it is possible for an applet to open a socket and listen on a port after it has been downloaded onto the client machine.

Sure you can, but it can only connect to the server it was downloaded from.

I suppose the server must have some way to push events to the applet through HTTP (to work across firewalls).
Thanks

HTTP is a bad choice for a protocol for what you want to do because it is a request-response protocol. You want a persistant connection.
Firewalls typically block incoming connections so there should be no problem connecting back to a server.


[How To Ask Questions On JavaRanch]
Shashank Agarwal
Ranch Hand

Joined: May 20, 2004
Posts: 105
Well you can easily push the events from the server to the applet. Use a stream for this. Let's understand this with an example.

You've got a chat applet and lets presume 10 ppl are logged on to the same chat server. Now, when one person writes a message and sends it to the server, the server simultaneously sends it to the other 9 applets. Use a simple TCP/IP connection. As Joe said, HTTP is a bad choice and try avoiding it unless impossible. You may use a thread and make it sleep before firing the next event, although it might be tricky to use with a chat server. Well, the server socket can wait till it recieves a prompt. You can make the applet to behave as a server and a client simultaneously.

Here's what i suggest -
Let the applet load and send the user's ip address to the server. This is on port 4001 (say). Now, the server, using another thread, behaves as a client and opens connection with the applet which now behaves as a client on port 4002(say, not 4001). So now u've got a two way connection. Both the applet and server have got a ServerSocket running to wait for the other to prompt it.

Got it. Simple, huh?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Server pushing events to Applet