event driven socket

Discussion in 'Java' started by asit, Aug 2, 2009.

  1. asit

    asit Guest

    Does socket can be associated with events ???

    My objective is to call a specific function when data arrives in a
    particular port ??
     
    asit, Aug 2, 2009
    #1
    1. Advertising

  2. asit

    Karl Uppiano Guest

    "asit" <> wrote in message
    news:...
    > Does socket can be associated with events ???


    The socket API (classic sockets or NIO) do not provide built-in Java Beans
    or Swing style event sources, but you can roll your own. You would need to
    define a class that contains the socket, and a thread to read the socket and
    fire an event when read returns with data.

    Your class would also have to provide the code to register and deregister
    listeners, and fire the event. The Java Beans specification has sample code
    that shows how to do that.

    Since this is a multi-threaded solution, you would also need to properly
    handle thread synchronization.

    > My objective is to call a specific function when data arrives in a
    > particular port ??


    Any listener that implements your defined event handler interface will run
    in response to the event.
     
    Karl Uppiano, Aug 2, 2009
    #2
    1. Advertising

  3. asit

    asit Guest

    How can this be done ???

    Give some more specific information ???
     
    asit, Aug 3, 2009
    #3
  4. asit

    Karl Uppiano Guest

    "asit" <> wrote in message
    news:...
    > How can this be done ???


    I usually start by looking at the JavaDocs for sockets, threads, etc., to
    understand the APIs, and by reading the relevant tutorials. For over ten
    years, java.sun.com has been my one-stop-shop for the majority of my Java
    knowledge. Read section 6 - Events from the Java Beans spec
    (http://java.sun.com/javase/technologies/desktop/javabeans/docs/spec.html)
    for sample code showing how to write event sources and how to define event
    listener interfaces.

    > Give some more specific information ???


    What would you like to know, specifically?

    Create a class to hold the socket and a worker thread. The class needs to
    provide a way to configure the socket (e.g., address:port, etc.) -- either
    constructor parameters or properties -- your choice, and it also needs a
    start or connect method, and probably a disconnect or close method.

    The worker thread opens the socket, and then spins in a loop, reading the
    input stream. Read blocks, waiting for data. When data is received, read
    returns with data. The thread then fires an event with the received data and
    whatever other information you want to include, and goes back to read again.
    Your thread needs an exception handler for the various socket I/O
    exceptions. The exception handler can also fire events to notify the
    listener of error conditions. Closing the socket from either end usually
    causes a blocked read to exit with an IOException, which your thread can use
    to fire a socket closed event.

    If you want to write to the socket, then you also need to define methods on
    your outer class to write data to the socket output stream.

    You have to do slightly different things if you have a server socket vs. a
    client socket. A server socket needs a worker thread that accepts
    connections, and then each connection might require its own thread to
    monitor that connection. I will leave server sockets as an exercise for the
    reader.
     
    Karl Uppiano, Aug 4, 2009
    #4
  5. asit

    Roedy Green Guest

    On Sun, 2 Aug 2009 09:43:33 -0700 (PDT), asit <>
    wrote, quoted or indirectly quoted someone who said :

    >Does socket can be associated with events ???


    In one app I did, I designed the C++ <-> Java protocol so that each
    message coming down the socket had its length on the front. I then
    knew how many bytes to read. I could then read that many bytes, and
    when I had them, I could grab a type field on the start of the message
    to tell me how to parse it into fields. Then I dispatched it to be
    parsed into an object, then the object representing the message was
    queued up for processing.

    You could also do it with a serialised object stream.

    You need a thread dedicated to reading the stream, that blocks waiting
    for a message to be read. That same thread may or may not do the
    parsing and processing of the message.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Patriotism is fierce as a fever, pitiless as the grave, blind as a stone, and as irrational as a headless hen."
    ~ Ambrose Bierce (born: 1842-06-24 died: 1914 at age: 71)
     
    Roedy Green, Aug 4, 2009
    #5
    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. Laszlo Nagy
    Replies:
    1
    Views:
    4,861
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    974
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    555
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    671
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    725
Loading...

Share This Page