1.IO通讯:
代码如下Client - Server:
(1)IoClient:
public class IOClient {
public static void main(String[] args) {
new Thread(() -> {
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 8000);
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
socket.getOutputStream().write((new Date() + ": hello world").getBytes());
socket.getOutputStream().flush();
//请求的时间间隔(5s)
Thread.sleep(5000);
} catch (Exception e) {
}
}
}).start();
}
}
(2)IoServer:
public class IOServer {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8000);
// (1) 接收新连接线程
new Thread(() -> {
while (true) {
try {
// (1) 阻塞方法获取新的连接
Socket socket = serverSocket.accept();
// (2) 每一个新的连接都创建一个线程,负责读取数据
new Thread(() -> {
try {
byte[] data = new byte[1024];
InputStream inputStream = socket.getInputStream();
while (true) {
int len;
// (3) 按字节流方式读取数据
while ((len = inputStream.read(data)) != -1) {
System.out.println(new String(data, 0, len));
}
}
} catch (IOException e) {
}
}).start();
} catch (IOException e) {
}
}
}).start();
}
}
注意:从一个阻塞的流中读数据,IO编程模型在客户端较少的情况下运行良好。
IO通信局限:
1.线程切换效率低
2.线程资源受限
3.数据读写是以字节流为单位,效率不高
2.NIO通讯:
1.Selector选择器
每当有客户端的连接过来,就会先把它注册到Selector中
2.Buffer缓冲区
提高读写性能
3.Channel多路复用
预先创建好链路,客户端连接时直接使用Channel中的链路即可。传输过程中的所有数据都是从Channel中走的。
(1)NIOClient:每个线程绑定一个轮询器selector,clientSelector负责轮询连接是否有数据可读
public class NIOClient {
public static void main(String[] args) throws IOException {
Selector clientSelector = Selector.open();
new Thread(() -> {
try {
while (true) {
// (2) 批量轮询是否有哪些连接有数据可读,这里的1指的是阻塞的时间为1ms
if (clientSelector.select(1) > 0) {
Set<SelectionKey> set = clientSelector.selectedKeys();
Iterator<SelectionKey> keyIterator = set.iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if (key.isReadable()) {
try {
SocketChannel clientChannel = (SocketChannel) key.channel();
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
// (3) 读取数据以块为单位批量读取
clientChannel.read(byteBuffer);
byteBuffer.flip();
System.out.println(Charset.defaultCharset().newDecoder().decode(byteBuffer)
.toString());
} finally {
keyIterator.remove();
key.interestOps(SelectionKey.OP_READ);
}
}
}
}
}
} catch (IOException ignored) {
}
}).start();
}
}
(2)NIOServer:每个线程绑定一个轮询器selector,serverSelector负责轮询是否有新的连接
public class NIOServer {
public static void main(String[] args) throws IOException {
Selector serverSelector = Selector.open();
Selector clientSelector = Selector.open();
new Thread(() -> {
try {
// 对应IO编程中服务端启动
ServerSocketChannel listenerChannel = ServerSocketChannel.open();
listenerChannel.socket().bind(new InetSocketAddress(8000));
listenerChannel.configureBlocking(false);
listenerChannel.register(serverSelector, SelectionKey.OP_ACCEPT);
while (true) {
// 监测是否有新的连接,这里的1指的是阻塞的时间为1ms
if (serverSelector.select(1) > 0) {
Set<SelectionKey> set = serverSelector.selectedKeys();
Iterator<SelectionKey> keyIterator = set.iterator();
while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();
if (key.isAcceptable()) {
try {
// (1) 每来一个新连接,不需要创建一个线程,而是直接注册到clientSelector
SocketChannel clientChannel = ((ServerSocketChannel) key.channel()).accept();
clientChannel.configureBlocking(false);
clientChannel.register(clientSelector, SelectionKey.OP_READ);
} finally {
keyIterator.remove();
}
}
}
}
}
} catch (IOException ignored) {
}
}).start();
}
}
注意:
NIO从一个通道里读数据,直到所有的数据都读到缓冲区里。
NIO 可让您只使用一个(或几个)单线程管理多个通道(网络连接或文件),但付出的代价是解析数据可能会比从一个阻塞流中读取数据更复杂。
3.Netty:
Netty是业界最流行的NIO框架之一,API使用简单、开发门槛低,功能强大,预置了多种编码解码功能,支持多种主流协议,性能高,与业界其他主流NIO框架对比,Netty性能最优。
Netty封装了JDK的NIO
Netty是一个异步事件驱动的网络应用框架
(1)NettyClient :
public class NettyClient {
public static void main(String[] args) throws InterruptedException {
Bootstrap bootstrap = new Bootstrap();
NioEventLoopGroup group = new NioEventLoopGroup();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) {
ch.pipeline().addLast(new StringEncoder());
}
});
Channel channel = bootstrap.connect("127.0.0.1", 8000).channel();
while (true) {
channel.writeAndFlush(new Date() + ": Hello , I'm NettyClient!");
Thread.sleep(5000);
}
}
}
(2)NettyServer :
public class NettyServer {
public static void main(String[] args) {
ServerBootstrap serverBootstrap = new ServerBootstrap();
NioEventLoopGroup boos = new NioEventLoopGroup();
NioEventLoopGroup worker = new NioEventLoopGroup();
serverBootstrap
//boos对应,IOServer.java中的接受新连接线程,主要负责创建新连接
//worker对应 IOClient.java中的负责读取数据的线程,主要用于读取数据以及业务逻辑处理
.group(boos, worker)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) {
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new SimpleChannelInboundHandler<String>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, String msg) {
System.out.println(msg);
}
});
}
})
.bind(8000);
}
}