Looking for a NIO client/server example

C

Chris Berg

My client/server application works with sockets, one for each user,
the sockets remain open for the entire session. Each client has a
separate thread on the server. A traditional design, but limited in
the max. number of conccurrent clients.

I think using non-blocking NIO should be able to greatly enhance the
system. However, there are two obstacles:

1) I need to re-write the thread code, especially the logic for
creation/destruction of a session, as there is now no thread that
governs the state of a user. This involves creating a state-vector and
a state-machine. But I can see a way through that.

2) I need to understand NIO in more detail. Looking at Sun's examples,
it looks quite a bit more complicates that old IO. Specifically, my
protocol has telegrams whose size can only be determined at run-time,
an that are in principle limitless. As I understand the NIO socket
handling, this complicates matters quite a bit as it uses fixed-size
buffers, but I may have misunderstood something.

So, my question:

Does anyone know of an example somewhere on the web that can be used
as a pattern?

Chris
 
C

Chris Berg

Oh, an other thing:

My clients may 'logoff' by simply disappearing, that is, without even
closing the Socket, just by stopping activity. In the old app, this is
easily dealt with using socket timeout; Sockets have no keep-alive
feature, so there are keep-alive dummy telegrams in my protocol. I
wonder if something similar can be done in NIO with Selectors?

Chris
 
V

Vincent Cantin

Chris Berg said:
My client/server application works with sockets, one for each user,
the sockets remain open for the entire session. Each client has a
separate thread on the server. A traditional design, but limited in
the max. number of conccurrent clients.

I think using non-blocking NIO should be able to greatly enhance the
system. However, there are two obstacles:

1) I need to re-write the thread code, especially the logic for
creation/destruction of a session, as there is now no thread that
governs the state of a user. This involves creating a state-vector and
a state-machine. But I can see a way through that.

2) I need to understand NIO in more detail. Looking at Sun's examples,
it looks quite a bit more complicates that old IO. Specifically, my
protocol has telegrams whose size can only be determined at run-time,
an that are in principle limitless. As I understand the NIO socket
handling, this complicates matters quite a bit as it uses fixed-size
buffers, but I may have misunderstood something.

So, my question:

Does anyone know of an example somewhere on the web that can be used
as a pattern?

Chris

Hi Chris,

Take a look on the JNAG project : https://jnag.dev.java.net/
Even if it is still in prototype version, it may help you to understand how
NIO works and how to use it.

Vincent
 
G

Gordon Beaton

My clients may 'logoff' by simply disappearing, that is, without
even closing the Socket, just by stopping activity. In the old app,
this is easily dealt with using socket timeout; Sockets have no
keep-alive feature, so there are keep-alive dummy telegrams in my
protocol. I wonder if something similar can be done in NIO with
Selectors?

When a remote *process* terminates (for any reason, including a crash)
it will appear to your application that it has abrubtly closed the
socket and you will detect EOF. This has nothing to do with NIO, it is
because the remote operating system closes the socket for it when the
process terminates.

There are other things that may occur that TCP does not know about and
therefore cannot tell you about, for example if the remote *host* dies
or someone unplugs a cable. Of course you can detect this with
heartbeat messages in your protocol as you are already doing, whether
you use NIO or not.

Note that Selector.select() lets you timeout an operation when there
has been no activity for some time.

/gordon
 

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

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top