本文整理匯總了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();
}
}
}