集册 Java实例教程 使用ServerSocketChannel创建服务器

使用ServerSocketChannel创建服务器

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

494
使用ServerSocketChannel创建服务器

import java.io.IOException;

import java.net.InetSocketAddress;

import java.net.StandardSocketOptions;/*时代Java - nowjava.com 提 供*/

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.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;


public class Main {

  private Map<SocketChannel, List<byte[]>> keepDataTrack = new HashMap<>();

  private ByteBuffer buffer = ByteBuffer.allocate(2 * 1024);


  private void startEchoServer() {

    try (Selector selector = Selector.open();
    /**
    来 自 时代Java
    **/

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open()) {

      if ((serverSocketChannel.isOpen()) && (selector.isOpen())) {

        serverSocketChannel.configureBlocking(false);

        serverSocketChannel.setOption(StandardSocketOptions.SO_RCVBUF,

            256 * 1024);

        serverSocketChannel.setOption(StandardSocketOptions.SO_REUSEADDR, true);

        serverSocketChannel.bind(new InetSocketAddress(5555));

        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        while (true) {

          selector.select();

          Iterator keys = selector.selectedKeys().iterator();

          while (keys.hasNext()) {

            SelectionKey key = (SelectionKey) keys.next();

            keys.remove();

            if (!key.isValid()) {

              continue;

            }

            if (key.isAcceptable()) {

              accept(key, selector);

            } else if (key.isReadable()) {

              read(key);

            } else if (key.isWritable()) {

              write(key);

            }

          }

        }

      } else {

        System.out.println("The server socket channel or selector cannot be opened!");

      }


    } catch (IOException ex) {

      System.err.println(ex);

    }

  }


  private void accept(SelectionKey key, Selector selector) throws IOException {

    ServerSocketChannel serverChannel = (ServerSocketChannel) key.channel();

    SocketChannel socketChannel = serverChannel.accept();

    socketChannel.configureBlocking(false);

    System.out.println("Incoming connection from: "

        + socketChannel.getRemoteAddress());

    socketChannel.write(ByteBuffer.wrap("Hello!\n".getBytes("UTF-8")));

    keepDataTrack.put(socketChannel, new ArrayList<byte[]>());

    socketChannel.register(selector, SelectionKey.OP_READ);

  }


  private void read(SelectionKey key) {

    try {

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

      buffer.clear();

      int numRead = -1;

      try {

        numRead = socketChannel.read(buffer);

      } catch (IOException e) {

        System.err.println("Cannot read error!");

      }

      if (numRead == -1) {

        this.keepDataTrack.remove(socketChannel);

        System.out.println("Connection closed by: "

            + socketChannel.getRemoteAddress());

        socketChannel.close();

        key.cancel();

        return;

      }

      byte[] data = new byte[numRead];

      System.arraycopy(buffer.array(), 0, data, 0, numRead);

      System.out.println(new String(data, "UTF-8") + " from "

          + socketChannel.getRemoteAddress());


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

      List<byte[]> channelData = keepDataTrack.get(s);

      channelData.add(data);


      key.interestOps(SelectionKey.OP_WRITE);

      

    } 
展开阅读全文