Java NIO SocketChannel
来源:互联网 发布:jetty nginx 负载均衡 编辑:程序博客网 时间:2024/06/06 14:10
SocketChannel
can be created:- You open a
SocketChannel
and connect to a server somewhere on the internet. - A
SocketChannel
can be created when an incoming connection arrives at aServerSocketChannel
.
Opening a SocketChannel
Here is how you open a SocketChannel
:
SocketChannel socketChannel = SocketChannel.open();socketChannel.connect(new InetSocketAddress("http://jenkov.com", 80));
Closing a SocketChannel
You close a SocketChannel
after use by calling theSocketChannel.close()
method. Here is how that is done:
socketChannel.close();
Reading from a SocketChannel
To read data from a SocketChannel
you call one of theread()
methods. Here is an example:
ByteBuffer buf = ByteBuffer.allocate(48);int bytesRead = socketChannel.read(buf);
First a Buffer
is allocated. The data read from theSocketChannel
is read into the Buffer
.
Second the SocketChannel.read()
method is called. This method reads data from theSocketChannel
into the Buffer
. The int
returned by theread()
method tells how many bytes were witten into the Buffer
. If -1 is returned, the end-of-stream is reached (the connection is closed).
Writing to a SocketChannel
Writing data to a SocketChannel
is done using theSocketChannel.write()
method, which takes a Buffer
as parameter. Here is an example:
String newData = "New String to write to file..." + System.currentTimeMillis();ByteBuffer buf = ByteBuffer.allocate(48);buf.clear();buf.put(newData.getBytes());buf.flip();while(buf.hasRemaining()) { channel.write(buf);}
Notice how the SocketChannel.write()
method is called inside a while-loop. There is no guarantee of how many bytes thewrite()
method writes to the SocketChannel
. Therefore we repeat thewrite()
call until the Buffer
has no further bytes to write.
Non-blocking Mode
You can set a SocketChannel
into non-blocking mode. When you do so, you can callconnect()
, read()
and write()
in asynchronous mode.
connect()
If the SocketChannel
is in non-blocking mode, and you callconnect()
, the method may return before a connection is established. To determine whether the connection is established, you can call thefinishConnect()
method, like this:
socketChannel.configureBlocking(false);socketChannel.connect(new InetSocketAddress("http://jenkov.com", 80));while(! socketChannel.finishConnect() ){ //wait, or do something else... }
write()
In non-blocking mode the write()
method may return without having written anything. Therefore you need to call thewrite()
method in a loop. But, since this is already being done in the previous write examples, no need to do anything differently here.
read()
In non-blocking mode the read()
method may return without having read any data at all. Therefore you need to pay attention to the returnedint
, which tells how many bytes were read.
Non-blocking Mode with Selectors
The non-blocking mode of SocketChannel
's works much better withSelector
's. By registering one or more SocketChannel
's with aSelector
, you can ask the Selector
for channels that are ready for reading, writing etc. How to useSelector
's with SocketChannel
's is explained in more detail in a later text in this tutorial.
- java nio socketChannel read
- Java NIO SocketChannel
- Java NIO SocketChannel
- Java NIO (八) SocketChannel
- Java NIO SocketChannel
- Java NIO SocketChannel
- Java NIO SocketChannel
- Java NIO之SocketChannel
- Java NIO 学习笔记 - SocketChannel
- java NIO教程(八)--SocketChannel
- java NIO 实现 SocketChannel通信
- Java NIO 之 ServerSocketChannel SocketChannel
- 【JAVA】【NIO】9、Java NIO SocketChannel
- Java Nio 九、Java NIO SocketChannel
- NIO SocketChannel
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(八) SocketChannel
- Java NIO系列教程(五) SocketChannel
- Apache参数KeepAlive该如何配置
- 二叉树及其应用--二叉树创建
- Shell while循环
- 反转字符串 不同方式
- C语言中字符串与整型,浮点型之间的转换
- Java NIO SocketChannel
- 复习(数据结构):线性表 : c++_标准写法
- Unable to add window --token is not valid
- MYSQL-----视图
- 制作验证码
- OC_NSTimeZone
- 最牛逼android上的图表库MpChart(一) 介绍篇
- java多态-示例
- 如何绘制caffe网络训练曲线