@@ -0,0 +1,3277 @@
+package com.mindincode.reactor.client;%0A%0A%0Aimport java.io.IOException;%0Aimport java.io.InterruptedIOException;%0Aimport java.net.InetSocketAddress;%0Aimport java.nio.ByteBuffer;%0Aimport java.nio.channels.SocketChannel;%0A%0A/**%0A * Created by yinsx on 2016/12/13.%0A */%0Apublic class ReactorClient %7B%0A    private int maxRead;%0A    private int maxWrite;%0A%0A    private ByteBuffer readBuffer;%0A    private ByteBuffer writeBuffer;%0A%0A    SocketChannel socket;%0A%0A    public ReactorClient() %7B%0A%0A    %7D%0A%0A    public void connect(String ip, int port) throws IOException %7B%0A        socket = SocketChannel.open();%0A        socket.configureBlocking(false);%0A        socket.connect(new InetSocketAddress(ip, port));%0A        int count = 1;%0A        while (!socket.finishConnect()) %7B%0A            if (count == 3) %7B%0A                break;%0A            %7D%0A            System.out.println(%22connect to %22 + ip);%0A            try %7B%0A                Thread.sleep(10);%0A            %7D catch (InterruptedException e) %7B%0A                throw new InterruptedIOException(e.toString());%0A            %7D%0A            count++;%0A%0A        %7D%0A        if (socket.isConnected()) %7B%0A            System.out.println(%22Successfully connect to %22 + ip);%0A        %7D else %7B%0A            System.out.println(%22Failed connect to %22 + ip);%0A        %7D%0A    %7D%0A%0A    public void read() throws IOException %7B%0A        readBuffer = ByteBuffer.allocate(getMaxRead());%0A        byte%5B%5D bytes = new byte%5BgetMaxRead()%5D;%0A        StringBuffer buf = new StringBuffer();%0A        if (!socket.isConnected()) %7B%0A            System.out.println(%22not connected%22);%0A            return;%0A        %7D%0A        int retRead = socket.read(readBuffer);%0A        for (; ; ) %7B%0A%0A            do %7B%0A                if (retRead %3E 0) %7B%0A                    readBuffer.flip();%0A                    int i = 0;%0A                    while (readBuffer.hasRemaining()) %7B%0A                        bytes%5Bi%5D = readBuffer.get();%0A                        i++;%0A                    %7D%0A%0A                    buf.append(new String(bytes));%0A                    readBuffer.clear();%0A                %7D%0A%0A                retRead = socket.read(readBuffer);%0A            %7D while (retRead != 0);%0A            System.out.println(%22Read: %22 + buf.toString());%0A        %7D%0A%0A%0A    %7D%0A%0A    public void write(String message) throws IOException %7B%0A        if (message == null %7C%7C %22%22.equals(message)) %7B%0A            return;%0A        %7D%0A        if (message.length() %3E getMaxWrite()) %7B%0A            System.out.println(%22messages are too long than %22 + getMaxWrite());%0A            return;%0A        %7D%0A        writeBuffer = ByteBuffer.allocate(message.getBytes().length);%0A        writeBuffer.clear();%0A        writeBuffer.put(message.getBytes());%0A        writeBuffer.flip();%0A        while (writeBuffer.hasRemaining()) %7B%0A            getSocket().write(writeBuffer);%0A        %7D%0A        System.out.println(%22Send Message:%22 + message);%0A    %7D%0A%0A    public int getMaxRead() %7B%0A        return maxRead;%0A    %7D%0A%0A    public int getMaxWrite() %7B%0A        return maxWrite;%0A    %7D%0A%0A    public SocketChannel getSocket() %7B%0A        return socket;%0A    %7D%0A%0A    public void setSocket(SocketChannel socket) %7B%0A        this.socket = socket;%0A    %7D%0A%0A    public void setMaxWrite(int maxWrite) %7B%0A        this.maxWrite = maxWrite;%0A    %7D%0A%0A    public void setMaxRead(int maxRead) %7B%0A        this.maxRead = maxRead;%0A    %7D%0A%7D

add client