Java NIO SocketChannel
2016-03-12 22:36
495 查看
Opening a SocketChannel
Closing a SocketChannel
Reading from a SocketChannel
Writing to a SocketChannel
Non-blocking Mode
connect()
write()
read()
Non-blocking Mode with Selectors
A Java NIO SocketChannel is a channel that is connected to a TCP network socket. It is Java NIO's equivalent of Java
Networking's Sockets. There are two ways a
You open a
A
Here is how you open a
You close a
To read data from a
First a
Second the
If -1 is returned, the end-of-stream is reached (the connection is closed).
Writing data to a
Notice how the
the
You can set a
like this:
differently here.
reading, writing etc. How to use
Closing a SocketChannel
Reading from a SocketChannel
Writing to a SocketChannel
Non-blocking Mode
connect()
write()
read()
Non-blocking Mode with Selectors
Jakob Jenkov Last update: 2014-06-25 |
Networking's Sockets. There are two ways a
SocketChannelcan be created:
You open a
SocketChanneland connect to a server somewhere on the internet.
A
SocketChannelcan be created when an incoming connection arrives at a
ServerSocketChannel.
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 SocketChannelafter use by calling the
SocketChannel.close()method. Here is how that is done:
socketChannel.close();
Reading from a SocketChannel
To read data from a SocketChannelyou call one of the
read()methods. Here is an example:
ByteBuffer buf = ByteBuffer.allocate(48); int bytesRead = socketChannel.read(buf);
First a
Bufferis allocated. The data read from the
SocketChannelis read into the
Buffer.
Second the
SocketChannel.read()method is called. This method reads data from the
SocketChannelinto the
Buffer. The
intreturned by the
read()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 SocketChannelis done using the
SocketChannel.write()method, which takes a
Bufferas 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 the
write()method writes to the
SocketChannel. Therefore we repeat the
write()call until
the
Bufferhas no further bytes to write.
Non-blocking Mode
You can set a SocketChannelinto non-blocking mode. When you do so, you can call
connect(),
read()and
write()in asynchronous mode.
connect()
If theSocketChannelis in non-blocking mode, and you call
connect(), the method may return before a connection is established. To determine whether the connection is established, you can call the
finishConnect()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 thewrite()method may return without having written anything. Therefore you need to call the
write()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 theread()method may return without having read any data at all. Therefore you need to pay attention to the returned
int, which tells how many bytes were read.
Non-blocking Mode with Selectors
The non-blocking mode ofSocketChannel's works much better with
Selector's. By registering one or more
SocketChannel's with a
Selector, you can ask the
Selectorfor channels that are ready for
reading, writing etc. How to use
Selector's with
SocketChannel's is explained in more detail in a later text in this tutorial.
相关文章推荐
- Java NIO FileChannel
- eclipse安装extjs插件
- java内部类的可见性总结和java内部类的创建
- Springmvc框架本地化和国际化
- JAVA设计模式--观察者
- Spring中的SpEL
- Java学习
- Java泛型通配符super使用Demo
- Java虚拟机内存模型
- java反射
- spring+shiro+cas的整合问题之循环重定向
- Java System.getProperty()方法获取系统信息
- Java之泛型练习
- Java基础复习系列三
- 20145232 韩文浩 《Java程序设计》第2周学习总结
- 了解HashMap的get和put内部的工作原理,需要理解透Java HashMap的原理
- Java中的除
- Java eclipse下 Ant build.xml实例详解 附完整项目源码
- java继承
- 简单探讨java中String类的hashCode()