Mixing NIO with IO

Posted by Steffen Heil on Stack Overflow See other posts from Stack Overflow or by Steffen Heil
Published on 2010-03-31T22:49:18Z Indexed on 2010/03/31 22:53 UTC
Read the original article Hit count: 534

Filed under:
|
|
|

Hi

Usually you have a single bound tcp port and several connections on these. At least there are usually more connections as bound ports. My case is different: I want to bind a lot of ports and usually have no (or at least very few) connections.

So I want to use NIO to accept the incoming connections.

However, I need to pass the accepted connections to the existing jsch ssh library. That requires IO sockets instead of NIO sockets, it spawns one (or two) thread(s) per connection. But that's fine for me.

Now, I thought that the following lines would deliver the very same result:

Socket a = serverSocketChannel.accept().socket();
Socket b = serverSocketChannel.socket().accep();
    SocketChannel channel = serverSocketChannel.accpet();
    channel.configureBlocking( true );
Socket c = channel.socket();
Socket d = serverSocket.accept();

However the getInputStream() and getOutputStream() functions of the returned sockets seem to work different. Only if the socket was accepted using the last call, jsch can work with it. In the first three cases, it fails (and I am sorry: I don't know why).

So is there a way to convert such a socket?

Regards, Steffen

© Stack Overflow or respective owner

Related posts about java

Related posts about nio