Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

How to listen out for a stream of data coming from (web)server

Thread view: 
Angus - 21 Nov 2006 19:22 GMT
Hello

I have an applet that connects to a server (same location as web server) and
connects to a server on a socket. This all works fine for sending commands
to this server. But the server can send data to the client at any moment in
time. So how do I listen out for the activity? do I launch a separate thread
that sits listening for incoming data?

What is the way to do it?

Angus
Oliver Wong - 21 Nov 2006 20:09 GMT
> Hello
>
[quoted text clipped - 6 lines]
> thread
> that sits listening for incoming data?

   Yes.

> What is the way to do it?

   See http://java.sun.com/docs/books/tutorial/networking/index.html

   - Oliver
Lew - 22 Nov 2006 04:23 GMT
> "Angus" <nospam@gmail.com> wrote
>> Hello
[quoted text clipped - 7 lines]
>> thread
>> that sits listening for incoming data?

>     Yes.

Or no.  Another approach is to have the other thread also be a "sender" to the
host, but the server only replies when it has something to say.  This saves
having to open a listening port on the client side, something that can hurt
when it bangs into a firewall.

It also means that the server doesn't have to know or hunt for ports on the
client. It just replies to a client that has declared itself ready for
instruction. This simplifies the server's code.

The server pseudo-code would be something like:

Set < Client > clients;
listen on ServerSocket
get a "READY FOR INSTRUCTION" connection from client foo
clients.add( foo );
...
when server has something to say to foo:
reply( foo, msg );
clients.remove( foo );

- Lew
Oliver Wong - 22 Nov 2006 15:09 GMT
>>> Hello
>>>
[quoted text clipped - 14 lines]
> saves having to open a listening port on the client side, something that
> can hurt when it bangs into a firewall.

   Right, if the client-server protocol is a "take turns to speak" type of
deal, you can save yourself some headaches using the method Lew proposed.
However, if the server can send data to the client at any moment in time,
having a seperate thread sitting around and listening for incoming data
sounds like a good idea to me.

   In the tutorial I linked to, there's an example of the client opening a
socket to the server (thus bypassing most firewall issues), and then using
that socket for both input and output.

   - Oliver
Angus - 25 Nov 2006 18:53 GMT
> >>> Hello
> >>>
[quoted text clipped - 26 lines]
>
>     - Oliver

The approach which I am trying out at the moment is for my applet to launch
a separate thread to handle the socket communication.  The server can send
information at any moment in time as you say.

But if I click on say a button in the applet, the button needs to send some
command to the server.  How do I send this command to the separate thread I
have spun off?

Sorry this is probably a really basic Java question but I am a beginner.  Do
I need to somehow post a message to the thread spun off?

Angus
Oliver Wong - 27 Nov 2006 17:09 GMT
> The approach which I am trying out at the moment is for my applet to
> launch
[quoted text clipped - 10 lines]
> Do
> I need to somehow post a message to the thread spun off?

   Lookup the "Producer/Consumer" problem. Your applet thread is the
producer, producing messages which it will put onto a message-queue. The
socket-handling thread is the consumer, removing messages from that queue,
and sending it out to the server.

   - Oliver


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.