Android与Java NIO实现简单Echo服务器与客户端

上一篇用Java IO来做了个Demo,于是乎进一步,用Java NIO来做一个。

NIO的优势在于非阻塞。使用了Selector在一个线程里进行轮询,就能够完成接入、收\发消息的操作,不需要每建立一个连接都新启动一个线程的方式。


Server端代码:

public class EchoServer {
    private static final int MAX_SIZE = 256; // max size 256
    private static Charset mCharSet = Charset.forName("UTF-8"); //encode and decode charset

    public static void main(String args[]) {
        try {
            Selector selector = Selector.open();    // init an selector
            initServerChannel(selector);
            startSelector(selector);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void initServerChannel(Selector selector) {
        ServerSocketChannel serverChannel = null;
        try {
            serverChannel = ServerSocketChannel.open();
            serverChannel.configureBlocking(false); // not blocking
            serverChannel.socket().bind(new InetSocketAddress(8999));

            serverChannel.register(selector, SelectionKey.OP_ACCEPT); // 将ServerChannl注册为accept感兴趣类型
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void startSelector(Selector selector) {
        int loopCount = 0;
        while (true) {
            int n = 0;    // Selector轮询注册来的Channel, 阻塞到至少有一个通道在你注册的事件上就绪了。
            try {
                n = selector.select();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (n == 0) {
                continue;
            }
            System.out.println("loopCount:" + loopCount);
            Iterator iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey selectionKey = iterator.next();    // 获取SelectionKey
                SocketChannel socketChannel = null;
                SelectableChannel selectableChannel = selectionKey.channel();

                // 每一步都要判断selectionKey.isValid(),避免断开连接产生的java.nio.channels.CancelledKeyException
                if (selectionKey.isValid() && selectionKey.isAcceptable()) {
                    System.out.println("selectionKey isAcceptable");
                    acceptClient(selectionKey, (ServerSocketChannel) selectableChannel);
                }
                if (selectionKey.isValid() && selectionKey.isReadable()) {
                    // a channel is ready for reading
                    System.out.println("selectionKey isReadable");
                    socketChannel = (SocketChannel) selectableChannel;// 返回为之创建此键的通道。
                    readMsg(selectionKey, socketChannel);
                }
                if (selectionKey.isValid() && selectionKey.isWritable()) {
                    // a channel is ready for writing
                    System.out.println("selectionKey isWritable");
                    socketChannel = (SocketChannel) selectableChannel;
                    writeMsg(selectionKey, socketChannel);
                }
                iterator.remove();
            }
            loopCount++;
        }
    }

    private static void acceptClient(SelectionKey selectionKey, ServerSocketChannel serverChannel) {
        // 此方法返回的套接字通道(如果有)将处于阻塞模式。
        try {
            SocketChannel socketChannel = serverChannel.accept();
            socketChannel.configureBlocking(false);

            // 向Selector注册Channel,设置读取为感兴趣操作,此类操作将会在下一次选择器select操作时被交付。同时附加byteBuffer对象作为数据传递的容器
            socketChannel.register(selectionKey.selector(), SelectionKey.OP_READ);

            System.out.println("connected from:" + socketChannel.getRemoteAddress());
        } catch (IOException e) {
            e.printStackTrace();
            selectionKey.cancel();
        }
    }

    private static void readMsg(SelectionKey selectionKey, SocketChannel socketChannel) {
        if (selectionKey == null || socketChannel == null) {
            return;
        }
        ByteBuffer dataBuffer = ByteBuffer.allocate(MAX_SIZE);
        int count = 0;
        try {
            count = socketChannel.read(dataBuffer);
        } catch (IOException e) {
            e.printStackTrace();
            selectionKey.cancel();
            try {
                socketChannel.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        if (count > 0) {
            dataBuffer.flip();
            byte[] bytes = new byte[dataBuffer.remaining()];
            dataBuffer.get(bytes);
            String data = new String(bytes, mCharSet);
            System.out.println("received: " + data);
            selectionKey.attach(dataBuffer);    // 给SelectionKey附加上的DataBuffer对象
            selectionKey.interestOps(SelectionKey.OP_WRITE);    // 读取完,设置写入为感兴趣操作,这样在接下来一个循环会触发到服务端写操作,给用户返回数据
        }
        if (count == -1) {
            selectionKey.cancel();
            try {
                socketChannel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void writeMsg(SelectionKey selectionKey, SocketChannel socketChannel) {
        if (selectionKey == null || socketChannel == null) {
            return;
        }
        ByteBuffer dataBuffer = (ByteBuffer) selectionKey.attachment();

        String data = new String(dataBuffer.array(), mCharSet);
        String result = "response: " + data.trim();
        dataBuffer.flip();
        System.out.println("send back: " + result);
        dataBuffer = ByteBuffer.wrap(result.getBytes(mCharSet));
        //输出到通道
        try {
            while (dataBuffer.hasRemaining()) {
                socketChannel.write(dataBuffer);
            }
            //将缓冲区的当前位置和界限之间的字节(如果有)复制到缓冲区的开始处
            dataBuffer.compact();
            selectionKey.interestOps(SelectionKey.OP_READ); // 写入完毕,设置读取为感兴趣操作
        } catch (IOException e) {
            e.printStackTrace();
            selectionKey.cancel();
            try {
                socketChannel.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
}

Android客户端:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private EditText mIpEt;
    private EditText mPortEt;
    private Button mConnBtn;
    private TextView mScreenTv;
    private EditText mInputEt;

    private Button mSendBtn;

    private SocketThread mSocketThread;
    private static Handler mMainHander;

    private static final int MSG_CONNECT = 0x001;
    private static final int MSG_RECEIVE = 0x002;
    private static final int MSG_SEND_ERROR = 0x003;

    private static final String DATA_RECEIVE = "data_receive";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mIpEt = findViewById(R.id.main_ip_et);
        mPortEt = findViewById(R.id.main_port_et);
        mConnBtn = findViewById(R.id.main_connect_btn);
        mScreenTv = findViewById(R.id.main_screen_tv);
        mInputEt = findViewById(R.id.main_input_et);
        mSendBtn = findViewById(R.id.main_send_btn);

        // defalut value. Change it to your own server ip
        mIpEt.setText("172.16.62.65");
        mPortEt.setText("8999");

        mConnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String ip = mIpEt.getText().toString();
                String port = mPortEt.getText().toString();
                if (TextUtils.isEmpty(ip) || TextUtils.isEmpty(port)) {
                    Toast.makeText(MainActivity.this, "ip or port is null", Toast.LENGTH_SHORT).show();
                } else {
                    connectToServer(ip, Integer.valueOf(port));
                }
            }
        });

        mSendBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data = mInputEt.getText().toString();
                if (!TextUtils.isEmpty(data)) {
                    mSocketThread.sendMsgToServer(data);
                }
            }
        });

        // TODO handler may cause memory leaks
        mMainHander = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_CONNECT:
                        Toast.makeText(MainActivity.this, "Connect to Server Success", Toast.LENGTH_SHORT).show();
                        mConnBtn.setText("Connected");
                        mConnBtn.setEnabled(false);
                        break;
                    case MSG_RECEIVE:
                        Bundle data = msg.getData();
                        String dataStr = data.getString(DATA_RECEIVE);
                        Log.i(TAG, "received data:" + dataStr);
                        CharSequence originData = mScreenTv.getText();
                        String result = originData + "\n" + dataStr;
                        mScreenTv.setText(result);
                        break;
                    case MSG_SEND_ERROR:
                        Toast.makeText(MainActivity.this, "Send Error, Connection may be Closed", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
    }

    private void connectToServer(String ip, int port) {
        mSocketThread = new SocketThread(ip, port);
        mSocketThread.start();
    }

    private static class SocketThread extends Thread {
        private static final int MAX_SIZE = 256; // max size 256
        private static Charset mCharSet = Charset.forName("UTF-8"); //encode and decode charset
        private String mIp;
        private int mPort;
        private SocketChannel mClientChannel;
        private Selector mSelector;

        public SocketThread(String ip, int port) {
            this.mIp = ip;
            this.mPort = port;
            try {
                mSelector = Selector.open();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            initClientChannel(mSelector);
            startSelector(mSelector);
        }

        private void initClientChannel(Selector selector) {
            try {
                mClientChannel = SocketChannel.open();
                mClientChannel.configureBlocking(false);
                mClientChannel.connect(new InetSocketAddress(mIp, mPort));
                mClientChannel.register(selector, SelectionKey.OP_CONNECT);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private void startSelector(Selector selector) {
            while (true) {
                int n = 0;    // Selector轮询注册来的Channel, 阻塞到至少有一个通道在你注册的事件上就绪了。
                try {
                    n = selector.select();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (n == 0) {
                    continue;
                }
                Iterator iterator = selector.selectedKeys().iterator();
                while (iterator.hasNext()) {
                    SelectionKey selectionKey = iterator.next();    // 获取SelectionKey

                    // 每一步都要判断selectionKey.isValid(),避免断开连接产生的java.nio.channels.CancelledKeyException
                    if (selectionKey.isValid() && selectionKey.isConnectable()) {
                        connectServer(selectionKey);
                    }
                    if (selectionKey.isValid() && selectionKey.isReadable()) {
                        // a channel is ready for reading
                        readMsg(selectionKey);
                    }
                    if (selectionKey.isValid() && selectionKey.isWritable()) {
                        // a channel is ready for writing
                    }
                    iterator.remove();
                }
            }
        }

        private void connectServer(SelectionKey selectionKey) {
            try {
                mClientChannel.finishConnect();
                mMainHander.sendEmptyMessage(MSG_CONNECT);
                selectionKey.interestOps(SelectionKey.OP_READ);
                Log.i(TAG, "connected to:" + mClientChannel.socket().getInetAddress());
            } catch (IOException e) {
                e.printStackTrace();
                selectionKey.cancel();
                try {
                    mClientChannel.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }

        private void readMsg(SelectionKey selectionKey) {
            if (selectionKey == null || mClientChannel == null) {
                return;
            }
            ByteBuffer dataBuffer = ByteBuffer.allocate(MAX_SIZE); // 从SelectionKey中取出注册时附加上的DataBuffer对象
            int count = 0;
            try {
                count = mClientChannel.read(dataBuffer);
            } catch (IOException e) {
                e.printStackTrace();
                selectionKey.cancel();
                try {
                    mClientChannel.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (count > 0) {
                dataBuffer.flip();
                byte[] bytes = new byte[dataBuffer.remaining()];
                dataBuffer.get(bytes);
                String data = new String(bytes, mCharSet);
                Message message = mMainHander.obtainMessage();
                message.what = MSG_RECEIVE;
                Bundle bundle = new Bundle();
                bundle.putString(DATA_RECEIVE, data);
                message.setData(bundle);
                mMainHander.sendMessage(message);
            }
        }

        public void sendMsgToServer(final String data) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    ByteBuffer dataBuffer = ByteBuffer.allocate(data.length());
                    dataBuffer.put(data.getBytes(mCharSet));
                    dataBuffer.flip();
                    //输出到通道
                    try {
                        while (dataBuffer.hasRemaining()) {
                            mClientChannel.write(dataBuffer);
                        }
                        Log.i(TAG, "send data" + data);
                    } catch (IOException e) {
                        e.printStackTrace();
                        mMainHander.sendEmptyMessage(MSG_SEND_ERROR);
                        try {
                            if (mClientChannel != null) {
                                mClientChannel.close();
                            }
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    }
}

MainActivity的布局文件:




    

    

    

最后,同样不要忘记清单文件中声明网络权限。

NIO的使用比IO要复杂一些,但是它非阻塞的特性,极大减少线程使用这些优势,还是很值得研究的。毕竟真正到了项目级别的代码,不可能用Java IO去实现,一定是基于NIO的网络框架。


Java NIO相关例子网上不太多,自己做的时候也踩了一些坑,尤其要注意ByteBuffer的使用方式,Java IO数据是面向流的,而NIO是面向Buffer的,Buffer的读写是基本功,如果有疑惑可以去查阅相关资料。也欢迎大家一起来探讨、学习~

你可能感兴趣的:(Android)