event driven socket

A

asit

Does socket can be associated with events ???

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

Karl Uppiano

asit said:
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.
 
K

Karl Uppiano

asit said:
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.
 
R

Roedy Green

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)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top