欢马劈雪     最近更新时间:2020-01-02 10:19:05

433
非阻塞套接字通道回显客户端程序

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;/** 来 自 时 代 J a v a**/

import java.net.InetAddress;

import java.net.InetSocketAddress;

import java.nio.ByteBuffer;

import java.nio.CharBuffer;

import java.nio.channels.SelectionKey;

import java.nio.channels.Selector;

import java.nio.channels.SocketChannel;

import java.nio.charset.Charset;

import java.nio.charset.CharsetDecoder;

import java.util.Iterator;

import java.util.Set;


public class Main {

  private static BufferedReader userInputReader = null;

  /*
   from nowjava.com 
  */

  public static void main(String[] args) throws Exception {

    InetAddress serverIPAddress = InetAddress.getByName("localhost");

    int port = 19000;

    InetSocketAddress serverAddress = new InetSocketAddress(serverIPAddress, port);


    Selector selector = Selector.open();


    SocketChannel channel = SocketChannel.open();

    channel.configureBlocking(false);

    channel.connect(serverAddress);


    int operations =

      SelectionKey.OP_CONNECT | SelectionKey.OP_READ | SelectionKey.OP_WRITE;

    channel.register(selector, operations);


    userInputReader = new BufferedReader(new InputStreamReader(System.in));

    while (true) {

      if (selector.select() > 0) {

        boolean doneStatus = processReadySet(selector.selectedKeys());

        if (doneStatus) {

          break;

        }

      }

    }


    channel.close();

  }


  public static boolean processReadySet(Set readySet) throws Exception {

    SelectionKey key = null;

    Iterator iterator = null;

    iterator = readySet.iterator();

    while (iterator.hasNext()) {

      key = (SelectionKey) iterator.next();


      iterator.remove();


      if (key.isConnectable()) {

        boolean connected = processConnect(key);

        if (!connected) {

          return true;

        }

      }


      if (key.isReadable()) {

        String msg = processRead(key);

        System.out.println("[Server]: " + msg);

      }


      if (key.isWritable()) {

        String msg = getUserInput();

        if (msg.equalsIgnoreCase("bye")) {

          return true; // Exit

        }

        processWrite(key, msg);

      }


    }


    return false;

  }


  public static boolean processConnect(SelectionKey key) {

    SocketChannel channel = (SocketChannel) key.channel();


    try {

      while (channel.isConnectionPending()) {

        channel.finishConnect();

      }

    }

    catch (IOException e) {

      key.cancel();

      e.printStackTrace();

      return false;

    }


    return true;

  }


  public static String processRead(SelectionKey key) throws Exception {

    SocketChannel sChannel = (SocketChannel) key.channel();

    ByteBuffer buffer = ByteBuffer.allocate(1024);

    sChannel.read(buffer);

    buffer.flip();

    Charset charset = Charset.forName("UTF-8");

    CharsetDecoder decoder = charset.newDecoder();

    CharBuffer charBuffer = decoder.decode(buffer);

  
展开阅读全文