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

Discussion in 'Java' started by Angus, Nov 21, 2006.

  1. Angus

    Angus Guest

    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
    Angus, Nov 21, 2006
    #1
    1. Advertising

  2. Angus

    Oliver Wong Guest

    "Angus" <> wrote in message
    news:ejvjln$5np$1$...
    > 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
    Oliver Wong, Nov 21, 2006
    #2
    1. Advertising

  3. Angus

    Lew Guest

    > "Angus" <> 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
    Lew, Nov 22, 2006
    #3
  4. Angus

    Oliver Wong Guest

    "Lew" <> wrote in message
    news:...
    >> "Angus" <> 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
    Oliver Wong, Nov 22, 2006
    #4
  5. Angus

    Angus Guest

    "Oliver Wong" <> wrote in message
    news:NKZ8h.160584$...
    >
    > "Lew" <> wrote in message
    > news:...
    > >> "Angus" <> 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
    Angus, Nov 25, 2006
    #5
  6. Angus

    Oliver Wong Guest

    "Angus" <> wrote in message
    news:eka3fb$mi8$1$...
    >
    > 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
    Oliver Wong, Nov 27, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael
    Replies:
    0
    Views:
    366
    Michael
    Mar 10, 2006
  2. Tim Tyler
    Replies:
    0
    Views:
    493
    Tim Tyler
    Jul 12, 2003
  3. Donny
    Replies:
    13
    Views:
    668
    Donny
    Aug 5, 2004
  4. Steve Holden

    PyCon is Coming! PyCon is Coming!

    Steve Holden, Jan 5, 2006, in forum: Python
    Replies:
    0
    Views:
    309
    Steve Holden
    Jan 5, 2006
  5. SABmore
    Replies:
    1
    Views:
    166
    Ing. Branislav Gerzo
    Jun 30, 2005
Loading...

Share This Page