0

我正在使用 java NIO 制作示例 p2p 网络并遇到一些麻烦。

这是场景

1 个节点同时具有客户端和服务器功能。我将 NIO 用于服务器功能,将普通套接字通道用于客户端

我在每个节点的 2 个独立线程中运行 NIO 服务器功能和客户端功能。NIO 服务器使用 ServerSocketChannel 和 selector

NIO 客户端将创建一个 SocketChannel 并连接到它要连接的节点的 ServerSocketChannel。

我的问题是:目前,发送方和接收方使用分离的端口。如何合并它们以仅使用 1 来发送和接收?

这是接收器的代码

 public Receiver(short port) {
    try {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel
                .open();
        serverSocketChannel.socket().setReuseAddress(true);
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.socket().bind(new InetSocketAddress(port));
        this.channel = serverSocketChannel;

        Selector selector = Selector.open();
        channel.configureBlocking(false);
        channel.register(selector, SelectionKey.OP_ACCEPT);

        p2pProtocol = new Protocol(port);
        while (true) {
            int readyChannels = selector.select();
            if (readyChannels == 0) {
                // Accept channel is busy
                continue;
            }

            Set<SelectionKey> selectedKeys = selector.selectedKeys();

            Iterator<SelectionKey> keyIterator = selectedKeys.iterator();

            while (keyIterator.hasNext()) {

                SelectionKey key = keyIterator.next();

                if (key.isAcceptable()) {
                    // a connection request from other node
                    acceptKey(selector, key);
                } else if (key.isConnectable()) {
                    // a connection was established with a remote server.
                    Log.writeLog("Connectable");

                } else if (key.isReadable()) {
                    // a channel is ready for reading
                    receive(key);

                } else if (key.isWritable()) {
                    // a channel is ready for writing
                    write(key);
                }

                keyIterator.remove();
            }
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这是给发件人的:

public Sender(InetAddress address, short remotePort, short serverPort) {
    this.listeningPort = serverPort;

    // Create socket connection
    try {
        socket = new Socket(address, remotePort);
        out = new DataOutputStream(socket.getOutputStream());
        in = new DataInputStream(new BufferedInputStream(
                socket.getInputStream()));
        active = true;
    } catch (IOException e) {
        e.printStackTrace();
        Log.writeLog("No I/O");
    }
}

public boolean getStatus()
{
    return active;
}

public Message sendMessage(MsgType type) {
    Message m = MessageFactory.craftRequestMessage(type,
            listeningPort, false);

    try {
        byte[] send = Conversion.serializeMessageToBytes(m).array();
        out.flush();
        out.write(send);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.writeLog(e.getMessage());
    }

    return m;
}
4

0 回答 0