java socket NIO2.0模式

public class TimeServer {

public static void main(String[] args) {

int port = 8080;

if(null != args && args.length > 0) {

try {

port = Integer.parseInt(args[0]);

} catch (NumberFormatException e) {

e.printStackTrace();

}

}

AsyncTimeServerHandler timeServer = new AsyncTimeServerHandler(port);

new Thread(timeServer, "AIO-AsyncTimeServerHandler-001").start();;

}

}

import java.io.IOException;

import java.net.InetSocketAddress;

import java.nio.channels.AsynchronousServerSocketChannel;

import java.util.concurrent.CountDownLatch;


public class AsyncTimeServerHandler implements Runnable {

private int port;

CountDownLatch latch;

AsynchronousServerSocketChannel asynchronousServerSocketChannel;


public AsyncTimeServerHandler(int port) {

this.port = port;

try {

asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open();

asynchronousServerSocketChannel.bind(new InetSocketAddress(this.port));

System.out.println("the time server is start in port "+this.port);

} catch (IOException e) {

e.printStackTrace();

}

}


@Override

public void run() {

latch = new CountDownLatch(1);

doAccept();

try {

latch.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

}


private void doAccept() {

asynchronousServerSocketChannel.accept(this, new AcceptCompletionHandler());

}


}

import java.nio.ByteBuffer;

import java.nio.channels.AsynchronousSocketChannel;

import java.nio.channels.CompletionHandler;


public class AcceptCompletionHandler implements CompletionHandler<AsynchronousSocketChannel, AsyncTimeServerHandler> {


@Override

public void completed(AsynchronousSocketChannel result, AsyncTimeServerHandler attachment) {

attachment.asynchronousServerSocketChannel.accept(attachment, this);

ByteBuffer buffer = ByteBuffer.allocate(1024);

result.read(buffer, buffer, new ReadCompletionHandler(result));

}


@Override

public void failed(Throwable exc, AsyncTimeServerHandler attachment) {

attachment.latch.countDown();

}


}

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.nio.ByteBuffer;

import java.nio.channels.AsynchronousSocketChannel;

import java.nio.channels.CompletionHandler;

import java.util.Date;


public class ReadCompletionHandler implements CompletionHandler<Integer, ByteBuffer> {

private AsynchronousSocketChannel channel;

public ReadCompletionHandler(AsynchronousSocketChannel channel) {

if(null == this.channel) {

this.channel = channel;

}

}


@Override

public void completed(Integer result, ByteBuffer attachment) {

attachment.flip();

byte[] body = new byte[attachment.remaining()];

attachment.get(body);

try {

String req = new String(body, "UTF-8");

System.out.println("The time server receive order :"+req);

String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(req) ? new Date(System.currentTimeMillis()).toString() : "BAD ORDER";

doWrite(currentTime);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}


private void doWrite(String currentTime) {

if(null != currentTime && currentTime.trim().length() > 0){

byte[] bytes = currentTime.getBytes();

ByteBuffer writeBuffer = ByteBuffer.allocate(bytes.length);

writeBuffer.put(bytes);

writeBuffer.flip();

channel.write(writeBuffer, writeBuffer, new CompletionHandler<Integer, ByteBuffer>(){


@Override

public void completed(Integer result, ByteBuffer buffer) {

//如果没有发送完成,则继续发送

if(buffer.hasRemaining()) {

channel.write(buffer, buffer, this);

}

}


@Override

public void failed(Throwable exc, ByteBuffer buffer) {

try {

channel.close();

} catch (IOException e) {

//e.printStackTrace();

}

}

});

}

}


@Override

public void failed(Throwable exc, ByteBuffer buffer) {

try {

this.channel.close();

} catch (IOException e) {

e.printStackTrace();

}

}


}

public class TimeClient {


public static void main(String[] args) {

int port = 8080;

if(null != args && args.length > 0) {

try {

port = Integer.parseInt(args[0]);

} catch (NumberFormatException e) {

e.printStackTrace();

}

}

new Thread(new AsyncTimeClientHandler("127.0.0.1", port), "AIO-AsyncTimeClientHandler-001").start();

}


}

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.channels.AsynchronousSocketChannel;

import java.nio.channels.CompletionHandler;

import java.util.concurrent.CountDownLatch;


public class AsyncTimeClientHandler implements CompletionHandler<Void, AsyncTimeClientHandler>, Runnable {

private String host;

private int port;

private AsynchronousSocketChannel client;

private CountDownLatch latch;


public AsyncTimeClientHandler(String host, int port) {

this.host = host;

this.port = port;

try {

client = AsynchronousSocketChannel.open();

} catch (IOException e) {

e.printStackTrace();

}

}



@Override

public void run() {

latch = new CountDownLatch(1);

client.connect(new InetSocketAddress(host, port), this, this);

try {

latch.await();

} catch (InterruptedException e) {

e.printStackTrace();

}

try {

client.close();

} catch (IOException e) {

e.printStackTrace();

}

}



@Override

public void completed(Void result, AsyncTimeClientHandler attachment) {

// TODO Auto-generated method stub

byte[] req = "QUERY TIME ORDER".getBytes();

ByteBuffer writeBuffer = ByteBuffer.allocate(req.length);

writeBuffer.put(req);

writeBuffer.flip();

client.write(writeBuffer, writeBuffer, new CompletionHandler<Integer, ByteBuffer>(){


@Override

public void completed(Integer result, ByteBuffer buffer) {

if(buffer.hasRemaining()) {

client.write(buffer, buffer, this);

}else{

ByteBuffer readBuffer = ByteBuffer.allocate(1024);

client.read(readBuffer, readBuffer, new CompletionHandler<Integer, ByteBuffer>(){


@Override

public void completed(Integer result, ByteBuffer buffer) {

buffer.flip();

byte[] bytes = new byte[buffer.remaining()];

buffer.get(bytes);

String body;

try {

body = new String(bytes, "UTF-8");

System.out.println("Now is : "+body);

latch.countDown();

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

}


@Override

public void failed(Throwable exc, ByteBuffer buffer) {

try {

client.close();

latch.countDown();

} catch (IOException e) {

//e.printStackTrace();

}

}

});

}

}


@Override

public void failed(Throwable exc, ByteBuffer buffer) {

try {

client.close();

latch.countDown();

} catch (IOException e) {

//e.printStackTrace();

}

}

});

}



@Override

public void failed(Throwable exc, AsyncTimeClientHandler attachment) {

try {

client.close();

latch.countDown();

} catch (IOException e) {

e.printStackTrace();

}

}


}

NIO2.0异步套接字通道是真正的异步非阻塞IO

你可能感兴趣的:(java socket NIO2.0模式)