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





PostPosted: 2006-11-22 3:22:00 Top

java-programmer, How to listen out for a stream of data coming from (web)server 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





PostPosted: 2006-11-22 4:10:00 Top

java-programmer >> How to listen out for a stream of data coming from (web)server
"Angus" <email***@***.com> wrote in message
news:ejvjln$5np$1$email***@***.com...
> 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?

Yes.

>
> What is the way to do it?

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

- Oliver


 
Lew





PostPosted: 2006-11-22 12:23:00 Top

java-programmer >> How to listen out for a stream of data coming from (web)server > "Angus" <email***@***.com> wrote
>> 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?

Oliver Wong wrote:
> 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





PostPosted: 2006-11-22 23:09:00 Top

java-programmer >> How to listen out for a stream of data coming from (web)server
"Lew" <email***@***.com> wrote in message
news:email***@***.com...
>> "Angus" <email***@***.com> wrote
>>> 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?
>
> Oliver Wong wrote:
>> 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.

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





PostPosted: 2006-11-26 2:53:00 Top

java-programmer >> How to listen out for a stream of data coming from (web)server
"Oliver Wong" <email***@***.com> wrote in message
news:NKZ8h.160584$email***@***.com...
>
> "Lew" <email***@***.com> wrote in message
> news:email***@***.com...
> >> "Angus" <email***@***.com> wrote
> >>> 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?
> >
> > Oliver Wong wrote:
> >> 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.
>
> 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
>
>

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





PostPosted: 2006-11-28 1:09:00 Top

java-programmer >> How to listen out for a stream of data coming from (web)server
"Angus" <email***@***.com> wrote in message
news:eka3fb$mi8$1$email***@***.com...
>
> 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?

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