使用nio实现web服务器
package com.nio; 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.Date;
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("127.0.0.1", 8080);
serverSocketChannel.socket().bind(address);
serverSocketChannel.configureBlocking(false);
serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
String content="";
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|SelectionKey.OP_WRITE);
System.out.println("Connected: " + socketChannel.socket().getRemoteSocketAddress());
} else if (selectionKey.isReadable()) {
SocketChannel socketChannel = (SocketChannel) selectionKey.channel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
String msg="";
while (socketChannel.read(buffer) > 0) {
buffer.flip();
byte[] dis = new byte[buffer.limit()];
buffer.get(dis);
content+=new String(dis);
buffer.flip();
msg+=new String(dis);
System.out.println("当前线程="+Thread.currentThread().getId()+"--"+new String(dis));
}
socketChannel.write(ByteBuffer.wrap((new Date()).toString().getBytes()));
socketChannel.close(); }
else if (selectionKey.isWritable()){
// String msg="";
// SocketChannel socketChannel=(SocketChannel)selectionKey.channel();
//
// socketChannel.write(ByteBuffer.wrap(("abc001"+msg).getBytes()));
// socketChannel.close(); } it.remove(); //若不删除,下次执行到selector.select 会持续返回0 造成 死循环
}
} Thread.sleep(100);
}
}
}
使用nio实现web服务器的更多相关文章
- reactor模式前序(二):NIO WEB服务器设计
前文介绍了传统IO的WEB经典服务器 reactor模式前序:传统IO的WEB服务器设计 下面看看JAVA NIO的WEB服务器设计 NIO是基于事件驱动的,对于NIO来说,重要组件是Selector ...
- 徒手用Java来写个Web服务器和框架吧<第一章:NIO篇>
因为有个不会存在大量连接的小的Web服务器需求,不至于用上重量级服务器,于是自己动手写一个服务器. 同时也提供了一个简单的Web框架.能够简单的使用了. 大体的需求包括 能够处理HTTP协议. 能够提 ...
- 关于Web服务器的认识
马上就要毕业了,也要开始找工作了,大学写了这么多代码了,却没有好好总结一下常用的概念很是遗憾额,就通过这篇博客记录一下我最常用的一些知识好了. 说到Web服务器,有很多文章都介绍的很好,之前看到一篇非 ...
- 深入理解Tornado——一个异步web服务器
本人的第一次翻译,转载请注明出处:http://www.cnblogs.com/yiwenshengmei/archive/2011/06/08/understanding_tornado.html原 ...
- tomcat解析之简单web服务器(图)
链接地址:http://gogole.iteye.com/blog/587163 之前有javaeyer推荐了一本书<how tomcat works>,今天晚上看了看,确实不错,第一眼就 ...
- 徒手用Java来写个Web服务器和框架吧<第三章:Service的实现和注册>
徒手用Java来写个Web服务器和框架吧<第一章:NIO篇> 徒手用Java来写个Web服务器和框架吧<第二章:Request和Response> 这一章先把Web框架的功能说 ...
- 徒手用Java来写个Web服务器和框架吧<第二章:Request和Response>
徒手用Java来写个Web服务器和框架吧<第一章:NIO篇> 接上一篇,说到接受了请求,接下来就是解析请求构建Request对象,以及创建Response对象返回. 多有纰漏还请指出.省略 ...
- WildFly8(JBoss)默认web服务器-------Undertow
Java微服务框架之Undertow 一.Undertow简介: Undertow 是红帽公司(RedHat)的开源产品,是 WildFly8(JBoos) 默认的 Web 服务器. 官网API给出一 ...
- nginx web服务器详解1(转)
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://freeloda.blog.51cto.com/2033581/1285332 大 ...
随机推荐
- GameObjectPool——Unity中的对象池
这里介绍一种对象池的写法.它的优点在于无论取出还是插入游戏物体都是常数量时间. using UnityEngine; using System.Collections; using System.Co ...
- TensorFlow进阶(六)---模型保存与恢复、自定义命令行参数
模型保存与恢复.自定义命令行参数. 在我们训练或者测试过程中,总会遇到需要保存训练完成的模型,然后从中恢复继续我们的测试或者其它使用.模型的保存和恢复也是通过tf.train.Saver类去实现,它主 ...
- 使用CSS3实现的player播放按钮
完成的效果如下 查看效果并下载 Step 1:先了解border的原理: Step 2:HTML代码结构 <section class="playContainer"> ...
- 华为E392-u92在 linux 2.6.34 内核环境下支持
还是先说说思路吧.网关需要支持4G LTE网卡.如之前的操作,插上网卡后,usb-modeswitch 后没有产生对应的/dev/ttyUSB0-2. 所以ppp拨号脚本等无法工作.这里在ubuntu ...
- SQL语言 之 事务控制
一.概述 事务是一些数据库操作的集合,这些操作由一组相关的SQL语句组成(只能是 DML 语句),它们是一个有机的整体,要么全部成功执行,要么全部不执行.事务时数据库并发控制和恢复技术的基本单位. 事 ...
- windows server 2008 安装Microsoft ActiveSync 6.1提示缺少一个Windows Mobile设备中心所须要的Windows组件
windows server 2008 安装WinCE的同步软件,须要安装Microsoft ActiveSync 6.1版本号的. 而不能安装ActiveSync|Microsoft ActiveS ...
- Android—构建安全的Androidclient请求,避免非法请求
今天通过实例来介绍一下怎样构建安全的Android客户端请求.避免非法请求: server端代码: 代码1-工具类: package com.ghj.p ...
- excel 获取中文拼音首字母
excel 获取中文拼音首字母 CreateTime--2018年5月31日08:50:42 Author:Marydon 1.情景展示 想要获取姓名的拼音首字母 2.实现方式 通过使用excel ...
- OFBiz:配置过程
OFBiz使用了大量的配置文件,整个过程有点复杂.这里将配置过程大略整理了一下,方便后面查阅. 第一层:org.ofbiz.base.start.Start启动类.该类载入org/ofbiz/base ...
- 常用/常见Java Web 服务器/应用服务器Logo图文介绍
常用/常见应用服务器介绍:自己总结.整理一些常见的应用服务器,资料主要来源于网上. 常用Java Web 服务器Java Web应用程序需要部署在Java web服务器中运行,常用的Java ...