socket非阻塞模式 java_非阻塞模式 Socket 连接

importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.InetAddress;importjava.net.InetSocketAddress;importjava.nio.channels.SelectionKey;importjava.nio.channels.Selector;importjava.nio.channels.SocketChannel;importjava.util.Iterator;importjava.util.LinkedList;classTarget {

InetSocketAddress address;

SocketChannel channel;

Exception failure;longconnectStart;long connectFinish = 0;boolean shown = false;

Target(String host) {try{

address= new InetSocketAddress(InetAddress.getByName(host), 80);

}catch(IOException e) {

failure=e;

}

}voidshow() {

String result;if(connectFinish != 0) {

result= Long.toString(connectFinish - connectStart) + "ms";

}else if(failure != null) {

result=failure.toString();

}else{

result= "Timed out";

}

System.out.println(address+ ":" +result);

shown= true;

}

}public classPingClient {privateSelector selector;private LinkedList targets = new LinkedList();private LinkedList finishedTargets = new LinkedList();boolean shutdown = false;public PingClient() throwsIOException {

selector=Selector.open();

Connector connector= newConnector();

Printer printer= newPrinter();

connector.start();

printer.start();

receiveTarget();

}public static void main(String[] args) throwsIOException {newPingClient();

}public voidaddTarget(Target target) {

SocketChannel socketChannel= null;try{

socketChannel=SocketChannel.open();

socketChannel.configureBlocking(false);

socketChannel.connect(target.address);

target.channel=socketChannel;

target.connectStart=System.currentTimeMillis();synchronized(targets) {

targets.add(target);

}

selector.wakeup();

}catch(Exception e) {if(socketChannel != null) {try{

socketChannel.close();

}catch(IOException ex) {

}

}

target.failure=e;

addFinishedTarget(target);

}

}public voidaddFinishedTarget(Target target) {synchronized(finishedTargets) {

finishedTargets.notify();

finishedTargets.add(target);

}

}public voidprintFinishedTargets() {try{for(;;) {

Target target= null;synchronized(finishedTargets) {while(finishedTargets.size() == 0){

finishedTargets.wait();

}

target=(Target) finishedTargets.removeFirst();

}

target.show();

}

}catch(InterruptedException e) {return;

}

}public voidregisterTargets() {synchronized(targets) {while(targets.size() > 0) {

Target target=(Target) targets.removeFirst();try{

target.channel.register(selector, SelectionKey.OP_CONNECT, target);

}catch(IOException e) {try{

target.channel.close();

}catch(IOException ex) {

ex.printStackTrace();

}

target.failure=e;

addFinishedTarget(target);

}

}

}

}public voidprocessSelectedKeys() {for(Iterator it =selector.selectedKeys().iterator(); it.hasNext();) {

SelectionKey selectionKey=it.next();

it.remove();

Target target=(Target) selectionKey.attachment();

SocketChannel socketChannel=(SocketChannel) selectionKey.channel();try{if(socketChannel.finishConnect()) {

selectionKey.cancel();

target.connectFinish=System.currentTimeMillis();

socketChannel.close();

addFinishedTarget(target);

}

}catch(IOException e) {try{

socketChannel.close();

}catch(IOException ex) {

ex.printStackTrace();

}

target.failure=e;

addFinishedTarget(target);

}

}

}public voidreceiveTarget() {try{

BufferedReader localReader= new BufferedReader(newInputStreamReader(System.in));

String host= null;while((host = localReader.readLine()) != null) {if(!host.equals("bye")) {

Target target= newTarget(host);

addTarget(target);

}else{

shutdown= true;

selector.wakeup();break;

}

}

}catch(IOException e) {

e.printStackTrace();

}

}public class Printer extendsThread {publicPrinter() {

setDaemon(true);

}public voidrun() {

printFinishedTargets();

}

}public class Connector extendsThread {public voidrun() {while(!shutdown) {try{

registerTargets();if(selector.select() > 0) {

processSelectedKeys();

}

}catch(Exception e) {

e.printStackTrace();

}

}try{

selector.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值