Java NIO示例

學習了Java的NIO框架,NIO是Java提供的非阻塞I/O,基於Channel和Buffer實現,下面給出Socket示例代碼,服務端和客戶端。

一、服務端代碼

import java.io.IOException;
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;


public class NIOServer
{
    /**
     * 選擇器
     */
    private Selector selector;

    /**
     * 通道
     */
    ServerSocketChannel serverSocketChannel;

    public void initServer(int port) throws IOException
    {
        //打開一個通道
        serverSocketChannel = ServerSocketChannel.open();

        //通道設置非阻塞
        serverSocketChannel.configureBlocking(false);

        //綁定端口號
        serverSocketChannel.socket().bind(new InetSocketAddress("localhost", port));

        //註冊
        this.selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    }

    public void listen() throws IOException
    {
        System.out.println("server started succeed!");

        while (true)
        {
            selector.select();
            Iterator<SelectionKey> ite = selector.selectedKeys().iterator();
            while (ite.hasNext())
            {
                SelectionKey key = ite.next();
                if (key.isAcceptable())
                {
                    SocketChannel channel = serverSocketChannel.accept();
                    channel.configureBlocking(false);
                    channel.register(selector, SelectionKey.OP_READ);
                }
                else if (key.isReadable())
                {
                    recvAndReply(key);
                }
                ite.remove();
            }
        }
    }

    public void recvAndReply(SelectionKey key) throws IOException
    {
        SocketChannel channel = (SocketChannel) key.channel();
        ByteBuffer buffer = ByteBuffer.allocate(256);
        int i = channel.read(buffer);
        if (i != -1)
        {
            String msg = new String(buffer.array()).trim();
            System.out.println("NIO server received message =  " + msg);
            System.out.println("NIO server reply =  " + msg);
            channel.write(ByteBuffer.wrap( msg.getBytes()));
        }
        else
        {
            channel.close();
        }
    }

    public static void main(String[] args) throws IOException
    {
        NIOServer server = new NIOServer();
        server.initServer(8080);
        server.listen();
    }
}

二、客戶端代碼

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;


public class NIOClient
{
    /**
     * 通道
     */
    SocketChannel channel;

    public void initClient(String host, int port) throws IOException
    {
        //構造socket連接
        InetSocketAddress servAddr = new InetSocketAddress(host, port);

        //打開連接
        this.channel = SocketChannel.open(servAddr);
    }

    public void sendAndRecv(String words) throws IOException
    {
        byte[] msg = new String(words).getBytes();
        ByteBuffer buffer = ByteBuffer.wrap(msg);
        System.out.println("Client sending: " + words);
        channel.write(buffer);
        buffer.clear();
        channel.read(buffer);
        System.out.println("Client received: " + new String(buffer.array()).trim());

        channel.close();
    }

    public static void main(String[] args) throws IOException
    {
        NIOClient client = new NIOClient();
        client.initClient("localhost", 8080);
        client.sendAndRecv("I am a client");
    }
}

三、測試

首先啓動服務端,再啓動客戶端,輸出如下:

服務端輸出

server started succeed!
NIO server received message =  I am a client
NIO server reply =  I am a client

客戶端輸出

Client sending: I am a client
Client received: I am a client

參考文檔

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章