八 SocketChannel

SocketChannel是一个连接到Tcp网络套接字的通道。可以通过以下两种方式创建SocketChannel:

  1、打开一个SocketChannel并连接到互联网上的某台服务器。

  2、一个新连接到达ServerSocketChannel时,会创建一个SocketChannel。

打开SocketChannel

SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress("http://jenkov.com",80));

关闭SocketChannel

socketChannel.close();

从socketChannel读取数据

ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = socketChannel.read(buf);

首先,分配一个Buffer。从SocketChannel读取到的数据将会放在这个Buffer中

然后,调用SocketChannel.read(),该方法将数据从SocketChannel读到Buffer中,read()返回的int值表示读了多少字节进Buffer里。如果返回的是-1,表示已经读到了流的末尾(连接关闭了)。

写入SocketChannel

String newData = "新String写入file。。。"+System.currentTimeMillis();

ByteBuffer buf = ByteBuffer.allocate(48);
buf.clear();
buf.put(newData.getBytes());

buf.flip();

while(buf.hasRemaining()){
      channel.write(buf);   
}

注意SocketChannel.write()方法的调用是一个while循环中的,write()方法无法保证能写多少字节到SocketChannel。所以,我们重复调用write()直到Buffer没有要写的字节为止。

非阻塞模式

可以设置SocketChannel为非阻塞模式(non-blocking mode)设置之后,就可以在异步模式下调用connect(),read()和write()了。

  connect()

如果SocketChannel在非阻塞模式下,此时调用connect(),该方法可能在连接建立之前就返回了。为了确定连接是否建立,可以调用finishConnect()的方法。

socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress("http://jenkov.com",80));

while(!socketChannel.finishConnect()){
      //do something   
}

write()

非阻塞模式下,write()在尚未写入任何内容时就可能返回了。所以需要在循环中调用write()。

read()

非阻塞模式下,read()方法在尚未读取到任何数据时可能就返回了。所以需要关注它的int返回值,它会告诉你读取了多少字节。

非阻塞模式与选择器

非阻塞模式与选择器搭配会工作的更好,通过一个或多个SocketChannel注册到Selector,可以询问选择器哪个通道已经准备好了读取,写入等

原文地址:https://www.cnblogs.com/gudulijia/p/6424209.html