天天看點

java aio http server_AIO實作簡單http伺服器

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;

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