Mercurial > hg > Members > you > BroadCastTreeVNC
changeset 4:657c691c2936
add readhandler.java
author | one |
---|---|
date | Mon, 23 Apr 2012 20:53:18 +0900 |
parents | 3b341997141a |
children | 970d5ac80256 |
files | src/treeVnc/ReadtHandler.java src/treeVnc/RfbProto.java |
diffstat | 2 files changed, 47 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/treeVnc/ReadtHandler.java Mon Apr 23 20:53:18 2012 +0900 @@ -0,0 +1,22 @@ +package treeVnc; + +import java.nio.channels.spi.AbstractSelectableChannel; + +public class ReadtHandler { + + private AbstractSelectableChannel channel; + + public ReadtHandler(RfbProto rfbProto, AbstractSelectableChannel ssChannel) { + this.setChannel(ssChannel); + + } + + public AbstractSelectableChannel getChannel() { + return channel; + } + + public void setChannel(AbstractSelectableChannel channel) { + this.channel = channel; + } + +}
--- a/src/treeVnc/RfbProto.java Mon Apr 23 20:18:23 2012 +0900 +++ b/src/treeVnc/RfbProto.java Mon Apr 23 20:53:18 2012 +0900 @@ -28,11 +28,15 @@ import java.io.*; import java.awt.event.*; +import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketException; import java.nio.ByteBuffer; +import java.nio.channels.DatagramChannel; +import java.nio.channels.SelectionKey; import java.nio.channels.ServerSocketChannel; +import java.nio.channels.SocketChannel; import java.nio.channels.spi.AbstractSelector; import java.nio.channels.spi.SelectorProvider; import java.util.LinkedList; @@ -236,8 +240,8 @@ timedKbits = 0; } - private ServerSocket newSocket(String host, int port) { - ServerSocketChannel ssChannel = SelectorProvider.provider().openServerSocketChannel(); + private Socket newSocket(String host, int port) throws IOException { + SocketChannel ssChannel = SelectorProvider.provider().openSocketChannel(); ssChannel.socket().setReuseAddress(true); // this should work for IPv6/IPv4 dual stack // check this using netstat -an result tcp46. @@ -249,6 +253,24 @@ ssChannel.socket().bind(new InetSocketAddress(port)); } ssChannel.configureBlocking(false); + ssChannel.register(selector, SelectionKey.OP_READ, new ReadtHandler(this, ssChannel)); + return ssChannel.socket(); + } + + public DatagramSocket newDatagramSocket(String host, int port) throws IOException { + DatagramChannel ssChannel = SelectorProvider.provider().openDatagramChannel(); + ssChannel.socket().setReuseAddress(true); + // this should work for IPv6/IPv4 dual stack + // check this using netstat -an result tcp46. + try { + InetSocketAddress address = new InetSocketAddress(host, port); + ssChannel.socket().bind(address); + } catch (SocketException e) { + // for some bad IPv6 implementation + ssChannel.socket().bind(new InetSocketAddress(port)); + } + ssChannel.configureBlocking(false); + ssChannel.register(selector, SelectionKey.OP_READ, new ReadtHandler(this, ssChannel)); return ssChannel.socket(); } @@ -1422,7 +1444,7 @@ numBytesRead += len; // System.out.println("numBytesRead:"+numBytesRead); } - + final int available() throws IOException { return is.available(); }