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服务器的更多相关文章

  1. reactor模式前序(二):NIO WEB服务器设计

    前文介绍了传统IO的WEB经典服务器 reactor模式前序:传统IO的WEB服务器设计 下面看看JAVA NIO的WEB服务器设计 NIO是基于事件驱动的,对于NIO来说,重要组件是Selector ...

  2. 徒手用Java来写个Web服务器和框架吧<第一章:NIO篇>

    因为有个不会存在大量连接的小的Web服务器需求,不至于用上重量级服务器,于是自己动手写一个服务器. 同时也提供了一个简单的Web框架.能够简单的使用了. 大体的需求包括 能够处理HTTP协议. 能够提 ...

  3. 关于Web服务器的认识

    马上就要毕业了,也要开始找工作了,大学写了这么多代码了,却没有好好总结一下常用的概念很是遗憾额,就通过这篇博客记录一下我最常用的一些知识好了. 说到Web服务器,有很多文章都介绍的很好,之前看到一篇非 ...

  4. 深入理解Tornado——一个异步web服务器

    本人的第一次翻译,转载请注明出处:http://www.cnblogs.com/yiwenshengmei/archive/2011/06/08/understanding_tornado.html原 ...

  5. tomcat解析之简单web服务器(图)

    链接地址:http://gogole.iteye.com/blog/587163 之前有javaeyer推荐了一本书<how tomcat works>,今天晚上看了看,确实不错,第一眼就 ...

  6. 徒手用Java来写个Web服务器和框架吧<第三章:Service的实现和注册>

    徒手用Java来写个Web服务器和框架吧<第一章:NIO篇> 徒手用Java来写个Web服务器和框架吧<第二章:Request和Response> 这一章先把Web框架的功能说 ...

  7. 徒手用Java来写个Web服务器和框架吧<第二章:Request和Response>

    徒手用Java来写个Web服务器和框架吧<第一章:NIO篇> 接上一篇,说到接受了请求,接下来就是解析请求构建Request对象,以及创建Response对象返回. 多有纰漏还请指出.省略 ...

  8. WildFly8(JBoss)默认web服务器-------Undertow

    Java微服务框架之Undertow 一.Undertow简介: Undertow 是红帽公司(RedHat)的开源产品,是 WildFly8(JBoos) 默认的 Web 服务器. 官网API给出一 ...

  9. nginx web服务器详解1(转)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://freeloda.blog.51cto.com/2033581/1285332 大 ...

随机推荐

  1. 关于Predicate<T>委托

    Predicate<T>委托在.NET类类库中经常出现,此委托的定义如下: public delegatebool Predicate<T>(T obj); 从其定义可以看到, ...

  2. DRP经验总结

    思想 指导 从开始看DRP项目到完成已经有三个月左右的时间了,这是一个足够长的视频,当看第一集的时候就再想,啥时候看完呢? 其间,也断断续续,有时看的效率高有时相反,有时几天看不了几集,好在总算看完了 ...

  3. Quadro P5200 - 最强大的移动工作站显卡 专门为了惠普 VR Z 背包电脑而发布

    https://www.leiphone.com/news/201708/Z1MCetuoobEaHIqa.html 前言 在今年的计算机图形技术顶会 SIGGRAPH,英伟达并不是在单纯地展示自家的 ...

  4. Hadoop I/O

    Hadoop自带一套原子操作用于数据I/O.当中一些技术,如数据完整性保持和压缩,对于处理多达数个TB的数据时.特别值得关注.另外一些Hadoop工具或API.所形成的构建模块可用于开发分布式系统.比 ...

  5. 【Python】Python中的深浅拷贝

    [转]python 复制(拷贝)对象 -- ::| 分类: Python |举报|字号 订阅 下载LOFTER我的照片书 | 需求: 你想复制一个对象.因为在Python中,无论你把对象做为参数传递, ...

  6. Android程序怎么做单元测试

    如何进行Android单元测试 Menifest.xml中加入: <application>中加入: <uses-library android:name="android ...

  7. Maven项目结合POI导出Excl表格Demo-亲测可用

    Maven项目结合POI导出Excl表格 一.POM文件添加依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> ...

  8. 退出app 退出应用程序

    退出app的两种方式1   创建activity的基类,让所有的activity都继承该基类,在基类中创建一个静态的activity列表,并在oncreate方法添加该activity,在退出时,遍历 ...

  9. es5 - array - join

    /** * join描述:将数组(或类数组对象)的所有元素连接到一个字符串中并返回此字符串. * join语法:arr .join([ separator]) * join参数:指定用于分隔数组的每对 ...

  10. QtGui.QSplitter

    A QtGui.QSplitter lets the user control the size of child widgets by dragging the boundary between t ...