利用NIO的Selector处理服务器-客户端模型
package NIOTEST; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set; public class NIOServer {
public static void main(String[] args) throws IOException, InterruptedException {
Selector selector = Selector.open(); ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), 8080);
serverSocketChannel.socket().bind(address);
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT); while (true) {
if (selector.select() > 0) {
Set<SelectionKey> selectionKeys = selector.selectedKeys();
Iterator<SelectionKey> it = selectionKeys.iterator();
while (it.hasNext()) {
SelectionKey selectionKey = it.next();
if (selectionKey.isAcceptable()) {
serverSocketChannel = (ServerSocketChannel)selectionKey.channel();
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.configureBlocking(false);
socketChannel.register(selector, SelectionKey.OP_READ);
System.out.println("Connected: " + socketChannel.socket().getRemoteSocketAddress());
} else if (selectionKey.isReadable()) {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (socketChannel.read(buffer) > 0) {
buffer.flip();
byte[] dis = new byte[buffer.limit()];
buffer.get(dis);
System.out.println("当前线程="+Thread.currentThread().getId()+"--"+new String(dis));
}
} it.remove();
}
} Thread.sleep(100);
}
}
}
客户端
package NIOTEST; import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
public class NIOClient {
public static void main(String[] args) throws IOException {
SocketChannel socketChannel = SocketChannel.open();
InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), 8080);
socketChannel.socket().connect(address); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); ByteBuffer buffer = ByteBuffer.allocate(1024);
while (true) {
try {
buffer.clear();
String time = sdf.format(new Date());
buffer.put(time.getBytes());
buffer.flip();
socketChannel.write(buffer);
Thread.sleep(5000);
} catch (Exception e) {
System.out.println("Connection Close");
break;
}
}
}
}
利用NIO的Selector处理服务器-客户端模型的更多相关文章
- 基于TCP协议 I/O多路转接(select) 的高性能回显服务器客户端模型
服务端代码: myselect.c #include <stdio.h> #include <netinet/in.h> #include <arpa/inet.h> ...
- Nio使用Selector客户端与服务器的通信
使用NIO的一个最大优势就是客户端于服务器自己的不再是阻塞式的,也就意味着服务器无需通过为每个客户端的链接而开启一个线程.而是通过一个叫Selector的轮循器来不断的检测那个Channel有消息处理 ...
- 利用NIO建立Socket服务器
传统的Java 的IO,利用Socket建立服务器,接收客户端连接,一般都是为每一个连接建立一个线程,如果连接数巨大,那么服务器开销也将巨大..NIO的原理,可以参照图:http://new.51ct ...
- java的nio之:java的nio的服务器实现模型
[nio服务端序列图]
- Java IO 与 NIO 服务器&客户端通信小栗子
本篇包含了入门小栗子以及一些问题的思考 BIO package com.demo.bio; import java.io.*; import java.net.ServerSocket; import ...
- NIO【同步非阻塞io模型】关于 NIO socket 的详细总结【Java客户端+Java服务端 + 业务层】【可以客户端间发消息】
1.前言 以前使用 websocket来实现双向通信,如今深入了解了 NIO 同步非阻塞io模型 , 优势是 处理效率很高,吞吐量巨大,能很快处理大文件,不仅可以 做 文件io操作, 还可以做sock ...
- Linux 下 简单客户端服务器通讯模型(TCP)
原文:Linux 下 简单客户端服务器通讯模型(TCP) 服务器端:server.c #include<stdio.h> #include<stdlib.h> #include ...
- 系统编程-网络-tcp客户端服务器编程模型(续)、连接断开、获取连接状态场景
相关博文: 系统编程-网络-tcp客户端服务器编程模型.socket.htons.inet_ntop等各API详解.使用telnet测试基本服务器功能 接着该上篇博文,咱们继续,首先,为了内容的完整性 ...
- NIO组件 Selector(选择器)
简介 使用Selector(选择器), 可以使用一个线程处理多个客户端连接. Selector 能够检测多个注册的通道上是否有事件发生(多个Channel以事件的方式可以注册到同一个Selector) ...
随机推荐
- Code+ B 汀博尔【二分答案】
时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K,其他语言524288K64bit IO Format: %lld 题目描述 有 n 棵树,初始时每棵树的高度为 Hi,第 ...
- Flash3D学习计划(一)——3D渲染的一般管线流程
一:什么是渲染管线 渲染管线也称为渲染流水线,是显示芯片内部处理图形信号相互独立的并行处理单元.一个流水线是一序列可以并行和按照固定顺序进行的阶段.每个阶段都从它的前一阶段接收输入,然后把输出发给随后 ...
- 3、列表 list
列表 >>> list=['aaa','bbb','ccc'] >>> print list ['aaa', 'bbb', 'ccc'] >>> ...
- 转换流的使用(InputStreamReader,OutputStreamWriter)
我们如何设置字节输入流.字节输出流的编码和解码格式? 在字节输入流(InputStream).字节输出流(OutputStream)中,并没有提供设置编码和解码格式的方法. InputStream的A ...
- Makefile之文件搜索
Makefile之文件搜索 1.Makefile 文件中的"VPATH"变量 如果没有指明这个变量,make只会在当前目录下查找依赖文件和目标文件: 如果定义了这个变量,make会 ...
- 关于国内外CV领域牛人的博客链接 .
此文为转载文章,尊重知识产权http://blog.csdn.net/carson2005/article/details/6601109此为原文链接,感谢作者! 以下链接是关于计算机视觉(Compu ...
- ES6中的Map集合(与java里类似)
Set类型可以用来处理列表中的值,但是不适用于处理键值对这样的信息结构.ES6也添加了Map集合来解决类似的问题 一.Map集合 JS的对象(Object),本质上是键值对的集合(Hash结构),但是 ...
- redhat mount iso as one yum repository
prepare redhat DVD iso rhel-server-6.4-x86_64-dvd.iso mount cd / mkdir /mnt/rhel mount -o loop rhel- ...
- 一分钟学awk够用
[转载于58同城沈剑] 1.什么是AWK(1)Aho.Weinberger.Kernighan三位发明者名字首字母:(2)一个行文本处理工具: 2.AWK基本原理2.1原理:逐行处理文件中的数据 2. ...
- 15 Basic ‘ls’ Command Examples in Linux
FROM: http://www.tecmint.com/15-basic-ls-command-examples-in-linux/ ls command is one of the most fr ...