Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
- From: Hal Vaughan <hal@xxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 27 Mar 2006 17:14:21 -0500
Daniel Dyer wrote:
On Mon, 27 Mar 2006 23:43:40 +0200, Hal Vaughan <hal@xxxxxxxxxxxxxxxxxxxx>
wrote:
Daniel Dyer wrote:
The reason is that it's not nearly as simple to do as you (and I) might
think it should be. It has only been possible since Java 5.0 and
requires
some effort to get it working properly. The key class is SSLEngine
(http://java.sun.com/j2se/1.5.0/docs/api/javax/net/ssl/SSLEngine.html).
Found the articles. Unfortunately, I'm stuck with 1.4.2. If I upgrade
to
1.5, I've got an entire application I've got to go through and upgrade to
compile on 1.5. While that will happen, I don't have time for it now.
(I'm not an early adopter!).
The problem is, if you want non-blocking I/O *AND* SSL, you don't have
much choice, it's Java 5.0 or give up.
That's what I figured after reading the article you linked to.
Okay, so one more question: I've seen programs that do close to what I'm
doing, which is echoing and/or forwarding the data from one port to the
next. One advantage to using non-blocking io (is nio short for
non-blocking io or network io?) is that it doesn't hog CPU time with
endless loops of reading a socket and writing to the other one while
there's nothing to write (as it keeps looping back checking for more
data).
I believe that NIO is short for the less informative, but equally
accurate, "New I/O" (somebody will correct me if I'm wrong).
As for CPU usage, it depends how you implement the blocking I/O. See this
article for your pre-5.0 options:
http://www.awprofessional.com/articles/printerfriendly.asp?p=167821&rl=1
It gives me about what I thought I'd have to do. At this point, I think
I'll need, at most, 2 threads. The ServerSocket gets one with an outer
loop and when it does a ServerSocket.accept(), it goes into the inner loop
to read. If the connection breaks, I break out of the inner loop and wait
for a new socket. (I'm only doing one socket at a time.) The other is for
the response/forwarding socket, which is a client socket, so it'll have 2
loops, one to read the data, with an outer one to re-establish the
socket/connection if it dies.
The main advantage of NIO for sockets is scalability. Depending on your
hardware, number of concurrent users and traffic patterns, you can
probably get by with the thread-per-session approach mentioned in the
article. Your threads (and therefore the CPU), will be (more-or-less)
idle when the sockets are idle. The downside is the memory footprint
impact of having lots of threads (and therefore lots of stacks) and the
cost of context switching.
On my end, everything has enough hardware to handle it. I'm more worried
about the people I'm working with. However, if scalability is the main
issue, two threads doing this shouldn't be a problem.
Am I right on that, or if I go back and re-write this under the older
style, so I loop and keep reading one inputstream and writing it to the
other socket's outputstream, isn't that going to steal a lot of CPU time
when there's no data coming in -- since it just keeps checking what's
available, getting a 0, then checking again?
No, the I/O blocks. The method call does not return until there is
some/enough data. While it is waiting the thread is effectively idle.
So if I do a loop to wait in an InputStream, it'll stop at the read method
until there is data -- is that correct?
Thanks!
If I had this info 2 weeks ago, I'd have had a day off by now!
Hal
.
- Follow-Ups:
- Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
- From: Daniel Dyer
- Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
- References:
- How Do I Use An SSLServerSocket With ServerSocketChannel?
- From: Hal Vaughan
- Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
- From: Daniel Dyer
- Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
- From: Hal Vaughan
- Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
- From: Daniel Dyer
- How Do I Use An SSLServerSocket With ServerSocketChannel?
- Prev by Date: Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
- Next by Date: Re: Install Sun Certificate on Remote PC
- Previous by thread: Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
- Next by thread: Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
- Index(es):
Relevant Pages
|