手记

socket的使用

/**
 * socket连接
 */
private void connectToServer() {

    NioSocketConnector connector = new NioSocketConnector();

    DefaultIoFilterChainBuilder chain = connector.getFilterChain();

    TextLineCodecFactory factory =
            new TextLineCodecFactory(Charset.forName(CHAR_SET));

    factory.setDecoderMaxLineLength(Integer.MAX_VALUE);
    factory.setEncoderMaxLineLength(Integer.MAX_VALUE);

    chain.addLast("codec", new ProtocolCodecFilter(factory));

    connector.setHandler(new DataCenterReceiveHandler(this));
    connector.setConnectTimeoutCheckInterval(30);
    Log.d(TAG, "begin connect server");

    ConnectFuture cf = connector.connect(
            new InetSocketAddress(getResString(R.string.socket_address), PORT));

    cf.awaitUninterruptibly();

    clientSession = cf.getSession();
    //connector.dispose();
    Log.d(TAG, "connect success ~");
}

原文链接:http://www.apkbus.com/blog-867174-62550.html

0人推荐
随时随地看视频
慕课网APP