Echo Client based on channel Selector - Java Network

Java examples for Network:URL Download

Description

Echo Client based on channel Selector

Demo Code


import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
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.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Date;
import java.util.Iterator;

public class NioEchoClient2 implements Runnable {

    private static final int PORT_NUMBER = 9111;
    private static final long TIME_OUT = 3000;

    private String mHostname;

    private Selector mSelector;

    private final ByteBuffer mReadBuffer = ByteBuffer.allocate(8192);
    private final ByteBuffer mWritBuffer = ByteBuffer.allocate(8192);

    private CharsetDecoder mDecoder;
    private CharsetEncoder mEncoder;
    private CharsetDecoder mMsDecoder;
    private InputThread mIt;
    private boolean mIsRunning;

    public NioEchoClient2(String host) {
        this.mHostname = host;

        this.mDecoder = Charset.forName("UTF-8").newDecoder();
        this.mEncoder = Charset.forName("UTF-8").newEncoder();
        this.mMsDecoder = Charset.forName("MS949").newDecoder();

    }/* w  w  w  . j a  v a2 s . co m*/

    SocketChannel mChannel = null;

    @Override
    public void run() {
        try {
            this.mSelector = Selector.open();
            mChannel = createSocketChannel();
            mChannel.register(mSelector, SelectionKey.OP_READ);

            mIt = new InputThread(this);
            mIt.start();

            mIsRunning = true;
        } catch (Exception e) {
            mIsRunning = false;
            e.printStackTrace();
        }

        while (mIsRunning) {
            try {
                mSelector.select(TIME_OUT);
                Iterator<SelectionKey> selectedKeys = mSelector
                        .selectedKeys().iterator();
                while (selectedKeys.hasNext()) {
                    SelectionKey key = selectedKeys.next();
                    selectedKeys.remove();
                    if (!key.isValid())
                        continue;

                    if (!key.isReadable()) {
                        continue;
                    }
                    readData(key);
                }
            } catch (EOFException e) {
                try {
                    mChannel.close();
                    mIt.shutdown();
                } catch (IOException t) {
                }
                mIsRunning = false;
            } catch (IOException e) {
                System.out.println("disconnected from server");
                e.printStackTrace();
                try {
                    mChannel.close();
                    mIt.shutdown();
                } catch (IOException t) {
                    t.printStackTrace();
                }
                mIsRunning = false;
            } finally {

            }
        }
        System.out.println("client exits");
    }

    public void sendMessage(String s) throws CharacterCodingException {
        CharBuffer cbuf = mMsDecoder.decode(ByteBuffer.wrap(s.getBytes()));
        ByteBuffer b = mEncoder.encode(cbuf);
        mWritBuffer.clear();
        mWritBuffer.put(b);
        mWritBuffer.putChar('\r');
        mWritBuffer.putChar('\n');
        mWritBuffer.flip();
        writeData();

    }
    private void writeData() {
        long nbytes = 0;
        long toWrite = mWritBuffer.remaining();
        try {
            while (nbytes != toWrite) {
                nbytes += mChannel.write(mWritBuffer);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void readData(SelectionKey key) throws IOException {
        SocketChannel channel = (SocketChannel) key.channel();
        mReadBuffer.clear();

        long nbytes = channel.read(mReadBuffer);
        if (nbytes < 0) {
            System.out.println("disconnected from server: end-of-stream");

            channel.close();
            mIt.shutdown();
            throw new EOFException();
        }

        mReadBuffer.flip();
        String line = mDecoder.decode(mReadBuffer).toString().trim();
        mReadBuffer.clear();
        System.out.println(line);
    }

    private SocketChannel createSocketChannel() throws IOException {
        SocketChannel client = SocketChannel.open(new InetSocketAddress(
                InetAddress.getByName(mHostname), PORT_NUMBER));
        client.configureBlocking(false);
        return client;
    }

    public static void main(String[] args) {
        String host = "192.168.0.3";

        try {
            NioEchoClient2 client = new NioEchoClient2(host);
            Thread t = new Thread(client);
            t.start();
            t.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    static class InputThread extends Thread {

        private NioEchoClient2 mCc;
        private BufferedReader mBr;
        private boolean mRunning;

        public InputThread(NioEchoClient2 nioEchoClient2) {
            this.mCc = nioEchoClient2;
            this.mBr = new BufferedReader(new InputStreamReader(System.in));
        }

        @Override
        public void run() {
            mRunning = true;
            while (mRunning) {
                try {
                    String s = this.mBr.readLine();
                    if (s.length() > 0) {
                        mCc.sendMessage(s);
                        if ("quit".equals(s)) {
                            mRunning = false;
                        }
                    }
                } catch (IOException e) {
                    mRunning = false;
                    e.printStackTrace();
                }
            }
        }

        public void shutdown() {
            mRunning = false;
            try {
                mBr.close();
            } catch (IOException e) {

                e.printStackTrace();
            }

            interrupt();
        }

    }

}

Related Tutorials