本文整理汇总了Java中java.nio.channels.SocketChannel.getRemoteAddress方法的典型用法代码示例。如果您正苦于以下问题:Java SocketChannel.getRemoteAddress方法的具体用法?Java SocketChannel.getRemoteAddress怎么用?Java SocketChannel.getRemoteAddress使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.SocketChannel
的用法示例。
在下文中一共展示了SocketChannel.getRemoteAddress方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: MysqlChannel
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
public MysqlChannel(SocketChannel channel) {
this.sequenceId = 0;
this.channel = channel;
this.sendBuffer = ByteBuffer.allocate(2 * 1024 * 1024);
this.isSend = false;
// get remote description
try {
if (channel.getRemoteAddress() instanceof InetSocketAddress) {
InetSocketAddress address = (InetSocketAddress) channel.getRemoteAddress();
remote = address.getAddress().getHostAddress() + ":" + address.getPort();
} else {
// Reach here, what's it?
remote = channel.getRemoteAddress().toString();
}
} catch (Exception e) {
remote = "";
}
}
示例2: accept
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
/**
* 接受新连接
*/
private void accept() {
SocketChannel channel = null;
try {
channel = serverChannel.accept();
channel.configureBlocking( false );
// 构建Connection
Connection c = factory.make(channel);
c.setDirection( Connection.Direction.in );
//c.setId( ConnectIdGenerator.getINSTNCE().getId() );
InetSocketAddress remoteAddr = (InetSocketAddress) channel.getRemoteAddress();
c.setHost(remoteAddr.getHostString());
c.setPort(remoteAddr.getPort());
// 将新连接派发至reactor进行异步处理
NIOReactor reactor = reactorPool.getNextReactor();
reactor.postRegister(c);
} catch (Exception e) {
LOGGER.warn(getName(), e);
closeChannel(channel);
}
}
示例3: addInNode
import java.nio.channels.SocketChannel; //导入方法依赖的package包/类
private void addInNode(SelectionKey key) {
SocketChannel socketChannel = null;
Node node = null;
try {
socketChannel = serverSocketChannel.accept();
InetSocketAddress socketAddress = (InetSocketAddress) socketChannel.getRemoteAddress();
if (!allowConnection(socketAddress)) {
socketChannel.close();
return;
}
socketChannel.configureBlocking(false);
SelectionKey newKey = socketChannel.register(selector, SelectionKey.OP_READ);
node = new Node(network, Node.IN, socketAddress);
nodesManager.addNodeToGroup(NetworkConstant.NETWORK_NODE_IN_GROUP, node);
ConnectionHandler handler = new ConnectionHandler(node, socketChannel, newKey);
node.setWriteTarget(handler);
newKey.attach(handler);
node.connectionOpened();
} catch (Exception e) {
if (socketChannel != null) {
Log.warn("in node Failed to connect" + node.getIp());
try {
socketChannel.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (node != null) {
node.destroy();
}
}
}