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

A

Angus

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
 
O

Oliver Wong

Angus said:
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
 
L

Lew

Oliver said:

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
 
O

Oliver Wong

Lew said:
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
 
A

Angus

Oliver Wong said:
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
 
O

Oliver Wong

Angus said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top