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. Advertisements

  2. asit

    Karl Uppiano Guest

    "asit" <> wrote in message
    > 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
    1. Advertisements

  3. asit

    asit Guest

    How can this be done ???

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

    Karl Uppiano Guest

    "asit" <> wrote in message
    > 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
    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
    Karl Uppiano, Aug 4, 2009
  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

    "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
    1. Advertisements

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. Peri
  2. Avizz
    Andy Fish
    Sep 29, 2003
  3. F
  4. christopher diggins
    christopher diggins
    Dec 21, 2004
  5. jose luis fernandez diaz

    Using signals as a event driven framework

    jose luis fernandez diaz, May 3, 2004, in forum: C Programming
    May 3, 2004
  6. Ben Kial
    Ben Kial
    Oct 12, 2003
  7. Laszlo Nagy
    Mark Wooding
    Jan 27, 2009
  8. Steve Holden