gpt4 book ai didi

java - 使用 NIO 的套接字中客户端的 IP 地址

转载 作者:行者123 更新时间:2023-12-02 07:47:11 26 4
gpt4 key购买 nike

使用 NIO,我们将两个端口绑定(bind)到 ServerSocket 类。

        serverChannelPrimary = ServerSocketChannel.open();
serverChannelSecondary = ServerSocketChannel.open();

// Retrieves a server socket associated with this channel
serverSocketPrimary = serverChannelPrimary.socket();
serverSocketSecondary = serverChannelSecondary.socket();

// Opens a connection selector
connectionSelector = Selector.open();

// Bind the specified port num
serverSocketPrimary.bind(new InetSocketAddress(portOne));
serverSocketSecondary.bind(new InetSocketAddress(portTwo));

// Set nonblocking mode for the listening socket
serverChannelPrimary.configureBlocking(false);
serverChannelSecondary.configureBlocking(false);

// Register the ServerSocketChannel with the Selector
serverChannelPrimary.register(connectionSelector, SelectionKey.OP_ACCEPT);
serverChannelSecondary.register(connectionSelector, SelectionKey.OP_ACCEPT);

现在,我们还能够在新客户端发出第一个请求时获取已连接客户端的 IP 地址,我们将其添加到 vector clientIps 中。

    while (isActive) {
try {
numberOfKeys = 0;
numberOfKeys = connectionSelector.select(timeOut);
if (numberOfKeys == 0) {
continue; // None of request available
}
// Get iterator through the selected keys list
Iterator<SelectionKey> iterKeys = connectionSelector
.selectedKeys().iterator();
while (iterKeys.hasNext()) {
try {
SelectionKey selectedKey = (SelectionKey) iterKeys
.next();
// Verify the key validity
if (!selectedKey.isValid()) {
logger.error("Received key is invalid");
continue;
} else if (selectedKey.isAcceptable()) {
// Accept the client request
ServerSocketChannel server = (ServerSocketChannel) selectedKey
.channel();
SocketChannel channel = server.accept();
// Get the socket associated with this channel
Socket clientInfo = channel.socket();
logger.debug("Application got client request from (Host name:"
+ clientInfo.getInetAddress().getHostName()
+ ",Ip address:"
+ clientInfo.getInetAddress()
.getHostAddress()
+ ",port:"
+ clientInfo.getPort());

String clientAddress=clientInfo.getInetAddress().getHostAddress();
if(!clientIps.contains(clientAddress)){
clientIps.add(clientAddress);
}

logger.debug("List of client : "+clientIps);

clientMgr.includeClient(channel);
}
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
logger.debug("Since this key has been handled, remove the SelectedKey from the selector list.");
iterKeys.remove();
}

}

} catch (Exception e) {
logger.error(e.getMessage());
}
}

但是,建立连接后,一旦我们开始在两个端口上从多个客户端获取数据,是否可以确定每当每个客户端发送数据时每个客户端的 IP 地址 。我希望我提供的代码足以且清晰地解释我们所遇到的情况。

最佳答案

ServerSocketChannel是TCP协议(protocol),所以两端的IP地址不能改变。

在你的行

SocketChannel channel = server.accept(); 

channel 特定于特定客户端。这些是您将用于与每个客户端通信的对象,每个对象都代表具有单个远程 IP/端口元组的单个 TCP session 。

关于java - 使用 NIO 的套接字中客户端的 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10664732/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com