/** * Attempts a new connection with the specified {@code remoteAddress} and * the current {@code "localAddress"} option. If the {@code "localAddress"} * option is not set, the local address of a new channel is determined * automatically. This method is identical with the following code: * * <pre> * {@link ClientBootstrap} b = ...; * b.connect(remoteAddress, b.getOption("localAddress")); * </pre> * * @return a future object which notifies when this connection attempt * succeeds or fails * * @throws ClassCastException * if {@code "localAddress"} option's value is * neither a {@link SocketAddress} nor {@code null} * @throws ChannelPipelineException * if this bootstrap's {@link #setPipelineFactory(ChannelPipelineFactory) pipelineFactory} * failed to create a new {@link ChannelPipeline} */ public ChannelFuture connect(SocketAddress remoteAddress) { if (remoteAddress == null) { throw new NullPointerException("remoteAddress"); } SocketAddress localAddress = (SocketAddress) getOption("localAddress"); return connect(remoteAddress, localAddress); }
/** * Attempts a new connection with the current {@code "remoteAddress"} and * {@code "localAddress"} option. If the {@code "localAddress"} option is * not set, the local address of a new channel is determined automatically. * This method is similar to the following code: * * <pre> * {@link ClientBootstrap} b = ...; * b.connect(b.getOption("remoteAddress"), b.getOption("localAddress")); * </pre> * * @return a future object which notifies when this connection attempt * succeeds or fails * * @throws IllegalStateException * if {@code "remoteAddress"} option was not set * @throws ClassCastException * if {@code "remoteAddress"} or {@code "localAddress"} option's * value is neither a {@link SocketAddress} nor {@code null} * @throws ChannelPipelineException * if this bootstrap's {@link #setPipelineFactory(ChannelPipelineFactory) pipelineFactory} * failed to create a new {@link ChannelPipeline} */ public ChannelFuture connect() { SocketAddress remoteAddress = (SocketAddress) getOption("remoteAddress"); if (remoteAddress == null) { throw new IllegalStateException("remoteAddress option is not set."); } return connect(remoteAddress); }
ChannelFuture future = bootstrap.connect(remoteAddr); future.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception {
if (!stopping) { LOG.debug("Connecting to " + remoteAddr); channelFuture = bootstrap.connect(remoteAddr);
public Future<ServerInfoMessage> requestInfo(final String address, final int port) { return pool.submit(() -> { InetSocketAddress remoteAddress = new InetSocketAddress(address, port); ChannelFuture connectCheck = bootstrap.connect(remoteAddress); connectCheck.syncUninterruptibly(); Channel channel = connectCheck.getChannel(); channel.getCloseFuture().syncUninterruptibly(); ServerInfoRequestHandler handler = channel.getPipeline().get(ServerInfoRequestHandler.class); ServerInfoMessage serverInfo = handler.getServerInfo(); return serverInfo; }); }
final int port = url.getPort() == -1 ? url.getDefaultPort() : url.getPort(); final ChannelFuture retVal; final ChannelFuture connectFuture = bootstrap.connect(new InetSocketAddress(host, port));
future = bootstrap.connect(serverAddress); future.addListener(new ChannelFutureListener()
bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); ChannelFuture connectCheck = bootstrap.connect(new InetSocketAddress(address, port)); try { connectCheck.await();
@Override protected void doConnect() throws Throwable { long start = System.currentTimeMillis(); ChannelFuture future = bootstrap.connect(getConnectAddress()); try { boolean ret = future.awaitUninterruptibly(getConnectTimeout(), TimeUnit.MILLISECONDS);
@Override protected void doConnect() throws Throwable { long start = System.currentTimeMillis(); ChannelFuture future = bootstrap.connect(getConnectAddress()); try { boolean ret = future.awaitUninterruptibly(getConnectTimeout(), TimeUnit.MILLISECONDS);
channelFuture = nettyClient.getBootstrap().connect( new InetSocketAddress(nettyClient.getUrl().getHost(), nettyClient.getUrl().getPort()));
private ChannelFuture connect(Request request, Uri uri, ProxyServer proxy, boolean useProxy, ClientBootstrap bootstrap, AsyncHandler<?> asyncHandler) throws UnknownHostException { InetSocketAddress remoteAddress = remoteAddress(request, uri, proxy, useProxy); if (asyncHandler instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(asyncHandler).onDnsResolved(remoteAddress.getAddress()); if (request.getLocalAddress() != null) return bootstrap.connect(remoteAddress, new InetSocketAddress(request.getLocalAddress(), 0)); else return bootstrap.connect(remoteAddress); }
@Override public ChannelFuture connect(ClientBootstrap bootstrap) { return bootstrap.connect(address); }
@Override public void run() { LOG.info("Reconnect ... [{}] to {}", tried_count, remote_addr); bootstrap.connect(remote_addr); }}, sleep); } else {
.connect(new InetSocketAddress(targetHost, tcpMeta.getTcpPort()));
/** * Connects to the server. */ @Override public synchronized void connect() { checkArgument(channel == null, "Client seems to already be connected."); channel = bootstrap.connect(new InetSocketAddress(host, port)).awaitUninterruptibly().getChannel(); }
@Override public void run() { bootstrap.connect(new InetSocketAddress(options.getHost(), options.getPort())); logger.info("spawned connection #{}", index); } });
private ChannelFuture connect(Request request, Uri uri, ProxyServer proxy, boolean useProxy, ClientBootstrap bootstrap, AsyncHandler<?> asyncHandler) throws UnknownHostException { InetSocketAddress remoteAddress = remoteAddress(request, uri, proxy, useProxy); if (asyncHandler instanceof AsyncHandlerExtensions) AsyncHandlerExtensions.class.cast(asyncHandler).onDnsResolved(remoteAddress.getAddress()); if (request.getLocalAddress() != null) return bootstrap.connect(remoteAddress, new InetSocketAddress(request.getLocalAddress(), 0)); else return bootstrap.connect(remoteAddress); }
public static void main(String args[]) { // Client服务启动器 ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // 设置一个处理服务端消息和各种消息事件的类(Handler) bootstrap.setPipelineFactory(() -> Channels.pipeline(new HelloClientHandler())); // 连接到本地的8000端口的服务端 bootstrap.connect(new InetSocketAddress("127.0.0.1", 8000)); }
private NodeChannels connectToChannelsLight(DiscoveryNode node) { InetSocketAddress address = ((InetSocketTransportAddress) node.address()).address(); ChannelFuture connect = clientBootstrap.connect(address); connect.awaitUninterruptibly((long) (connectTimeout.millis() * 1.5)); if (!connect.isSuccess()) { throw new ConnectTransportException(node, "connect_timeout[" + connectTimeout + "]", connect.getCause()); } Channel[] channels = new Channel[1]; channels[0] = connect.getChannel(); channels[0].getCloseFuture().addListener(new ChannelCloseListener(node)); return new NodeChannels(channels, channels, channels, channels, channels); }