Channel类

I/O可以广义的分为两类:File I/O和Stream I/O。那么相应的就有两种类型的通道,它们是文件通道和套接字通道,在java.nio.channels包下面你会发现FileChannel类和三个socket通道类:SocketChannel、ServerSocketChannel、DatagramChannel。

通道可以有多种方式创建,Socket通道有可以直接创建新socket通道的方法,但是一个FileChannel对象只能通过一个打开的RandomAccessFile、FileInputStream和FileOutputStream对象上调用getChannel()方法来获取:

SocketChannel sc = SocketChannel.open();
sc.connect(new InetSocketAddress(host,port));

 

ServerSocketChannel ssc = ServerSocketChannel.open();
ssc.socket.bind(new InetSocketAddress(localport));
 
DatagramChannel dc = DatagramChannel.open();
 
RandomAccessFile raf = new RandomAccessFile("file","r");
FileChannel fc = raf.getChannel();

 

通道是将数据传输给ByteBuffer对象或者从ByteBuffer对象获取数据进行传输。


Channel类

你可能感兴趣的:(socket)