Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Server pushing events to Applet

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 9612
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Would you like to try a free sample? Today we are featuring tiny ads:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic