Android Java TCP / IP套接字连接生命周期

时间:2012-03-02 02:11:16

标签: java android tcp connection nio

我在android上需要一些实现TCP / IP(显然是由Java) 我遇到了http://rox-xmlrpc.sourceforge.net/niotut/index.html

然而,我想知道它是否是正确的方法。 如果您查看代码,您会注意到客户端http://rox-xmlrpc.sourceforge.net/niotut/src/NioClient.java每次向服务器发送请求时都会创建新连接。

public void send(byte[] data, RspHandler handler) throws IOException {
    // Start a new connection
    SocketChannel socket = this.initiateConnection();

    // Register the response handler
    this.rspHandlers.put(socket, handler);

    // And queue the data we want written
    synchronized (this.pendingData) {
        List queue = (List) this.pendingData.get(socket);
        if (queue == null) {
            queue = new ArrayList();
            this.pendingData.put(socket, queue);
        }
        queue.add(ByteBuffer.wrap(data));
    }

    // Finally, wake up our selecting thread so it can make the required changes
    this.selector.wakeup();
}

但我不认为应该这样做。任何人都可以建议我如何改善这个? 提前谢谢!

0 个答案:

没有答案
相关问题