Java 非阻塞套接字

2018-09-14 17:06 更新

Java網(wǎng)絡(luò)教程 - Java非阻塞套接字


使用非阻塞套接字通道,我們必須改變我們對(duì)執(zhí)行順序的思考方式。

服務(wù)器套接字通道

要?jiǎng)?chuàng)建選擇器對(duì)象,請(qǐng)調(diào)用其open()靜態(tài)方法。

Selector selector = Selector.open();

ServerSocketChannel用于監(jiān)聽(tīng)來(lái)自客戶端的新連接請(qǐng)求。

調(diào)用其open()靜態(tài)方法來(lái)創(chuàng)建一個(gè)ServerSocketChannel。

ServerSocketChannel ssChannel = ServerSocketChannel.open();

默認(rèn)情況下,服務(wù)器套接字通道或套接字通道是阻塞通道。要使其成為非阻塞通道,請(qǐng)調(diào)用以下方法。

ssChannel.configureBlocking(false);

選擇器

服務(wù)器套接字必須向選擇器注冊(cè)才能執(zhí)行某些操作。

有四種操作,我們可以用選擇器注冊(cè)一個(gè)通道。

  • 使用SelectionKey.OP_CONNECT連接操作,可以在客戶端為SocketChannel注冊(cè)。選擇器將通知有關(guān)連接操作進(jìn)度。
  • 使用SelectionKey.OP_ACCEPT接受操作,可以在服務(wù)器上為ServerSocketChannel注冊(cè)。當(dāng)客戶端請(qǐng)求新連接到達(dá)時(shí),選擇器將通知。
  • 使用SelectionKey.OP_READ讀取操作,可以在客戶端和服務(wù)器上為SocketChannel注冊(cè)。選擇器將在通道準(zhǔn)備好讀取某些數(shù)據(jù)時(shí)通知。
  • 使用SelectionKey.OP_WRITE進(jìn)行寫操作,可以在客戶端和服務(wù)器上為SocketChannel注冊(cè)。選擇器將在通道準(zhǔn)備好寫入某些數(shù)據(jù)時(shí)通知。

例子

以下代碼顯示如何創(chuàng)建非阻塞套接字通道回顯服務(wù)器程序。

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Set;

public class Main {
  public static void main(String[] args) throws Exception {
    InetAddress hostIPAddress = InetAddress.getByName("localhost");
    int port = 19000;
    Selector selector = Selector.open();
    ServerSocketChannel ssChannel = ServerSocketChannel.open();
    ssChannel.configureBlocking(false);
    ssChannel.socket().bind(new InetSocketAddress(hostIPAddress, port));
    ssChannel.register(selector, SelectionKey.OP_ACCEPT);
    while (true) {
      if (selector.select() <= 0) {
        continue;
      }
      processReadySet(selector.selectedKeys());
    }
  }
  public static void processReadySet(Set readySet) throws Exception {
    Iterator iterator = readySet.iterator();
    while (iterator.hasNext()) {
      SelectionKey key = (SelectionKey) iterator.next();
      iterator.remove();
      if (key.isAcceptable()) {
        ServerSocketChannel ssChannel = (ServerSocketChannel) key.channel();
        SocketChannel sChannel = (SocketChannel) ssChannel.accept();
        sChannel.configureBlocking(false);
        sChannel.register(key.selector(), SelectionKey.OP_READ);
      }
      if (key.isReadable()) {
        String msg = processRead(key);
        if (msg.length() > 0) {
          SocketChannel sChannel = (SocketChannel) key.channel();
          ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
          sChannel.write(buffer);
        }
      }
    }
  }
  public static String processRead(SelectionKey key) throws Exception {
    SocketChannel sChannel = (SocketChannel) key.channel();
    ByteBuffer buffer = ByteBuffer.allocate(1024);
    int bytesCount = sChannel.read(buffer);
    if (bytesCount > 0) {
      buffer.flip();
      return new String(buffer.array());
    }
    return "NoMessage";
  }
}

例2

非阻塞套接字通道回顯客戶端程序

import java.io.BufferedReader;
import java.io.InputStreamReader;
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;
/*from www.o2fo.com*/
public class Main {
  static BufferedReader userInputReader = null;

  public static boolean processReadySet(Set readySet) throws Exception {
    Iterator iterator = readySet.iterator();
    while (iterator.hasNext()) {
      SelectionKey key = (SelectionKey)
       iterator.next();
      iterator.remove();
      if (key.isConnectable()) {
        boolean connected = processConnect(key);
        if (!connected) {
          return true; // Exit
        }
      }
      if (key.isReadable()) {
        String msg = processRead(key);
        System.out.println("[Server]: " + msg);
      }
      if (key.isWritable()) {
        System.out.print("Please enter a message(Bye to quit):");
        String msg = userInputReader.readLine();
        
        if (msg.equalsIgnoreCase("bye")) {
          return true; // Exit
        }
        SocketChannel sChannel = (SocketChannel) key.channel();
        ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
        sChannel.write(buffer);
      }
    }
    return false; // Not done yet
  }
  public static boolean processConnect(SelectionKey key) throws Exception{
    SocketChannel channel = (SocketChannel) key.channel();
    while (channel.isConnectionPending()) {
      channel.finishConnect();
    }
    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);
    String msg = charBuffer.toString();
    return msg;
  }
  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();
  }
}
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)