java aio http server_AIO实现简单http服务器

这是一个使用Java AIO实现的简单HTTP服务器示例,能够响应GET请求并返回固定内容。服务器创建了一个异步通道管理器和固定大小的线程池,监听8888端口,并通过CompletionHandler处理客户端连接,发送HTTP响应头和内容。
摘要由CSDN通过智能技术生成

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.AsynchronousChannelGroup;

import java.nio.channels.AsynchronousServerSocketChannel;

import java.nio.channels.AsynchronousSocketChannel;

import java.nio.channels.CompletionHandler;

import java.util.concurrent.ExecutionException;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.Future;

/**

* 一个简单的web 服务器

* 通过浏览器输入localhost:8080/访问

*

* @author Joeson

* @since 2014/05

*

*/

public class AioServer implements Runnable

{

private AsynchronousChannelGroup asyncChannelGroup;

private AsynchronousServerSocketChannel server;

public AioServer(int port) throws Exception

{

// 创建线程池

ExecutorService executor = Executors.newFixedThreadPool(20);

// 异步通道管理器

asyncChannelGroup = AsynchronousChannelGroup.withThreadPool(executor);

// 创建 用在服务端的异步Socket.以下简称服务器socket。

// 异步通道管理器,会把服务端所用到的相关参数

server = AsynchronousServerSocketChannel.open(asyncChannelGroup).bind(

new InetSocketAddress(port));

}

public void run()

{

try

{

// 为服务端socket指定接收操作对象.accept原型是:

// accept(A attachment, CompletionHandler

// ? super A> handler)

// 也就是这里的CompletionHandler的A型参数是实际调用accept方法的第一个参数

// 即是listener。另一个参数V,就是原型中的客户端socket

server.accept(

server,

new CompletionHandler()

{

String str = "

test

this is a socketserver test

";

String CRLF = "\r\n";

// 响应头的参数

String serverLine = "Server:a simple java WebServer";

String statusLine = "HTTP/1.1 200 OK" + CRLF;

String contentTypeLine = "Content-type:text/html"

+ CRLF;

String contentLengthLine = "Content-Length:" + str.length()

+ CRLF;

@Override

public void completed(AsynchronousSocketChannel result,

AsynchronousServerSocketChannel attachment)

{

// TODO Auto-generated method stub

// writeChannel(result, statusLine);

// writeChannel(result, serverLine);

// writeChannel(result, contentTypeLine);

// writeChannel(result, contentLengthLine);

// writeChannel(result, CRLF);

writeChannel(result, statusLine + serverLine

+ contentTypeLine + contentLengthLine

+ CRLF + str);

// writeChannel(result, str);

try

{

result.shutdownOutput();

result.shutdownInput();

} catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

try

{

result.close();

} catch (IOException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

attachment.accept(attachment, this);

}

@Override

public void failed(Throwable exc,

AsynchronousServerSocketChannel attachment)

{

// TODO Auto-generated method stub

}

public void writeChannel(

AsynchronousSocketChannel channel, String s)

{

Future future = channel.write(ByteBuffer

.wrap(s.getBytes()));

try

{

future.get();

} catch (InterruptedException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ExecutionException e)

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

Thread.sleep(400000);

} catch (Exception e)

{

e.printStackTrace();

} finally

{

System.out.println("finished server");

}

}

public static void main(String... args) throws Exception

{

AioServer server = new AioServer(8888);

new Thread(server).start();

}

}

标签:http,String,java,AIO,result,writeChannel,import,catch,服务器

来源: https://www.cnblogs.com/ZCWang/p/12906853.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值