src/main/java/hu/trigary/simplenetty/client/Client.java
/**
	 * Connect to the server synchronously. Once it is completed, the client is ready to send and receive data.
	 *
	 * @param host the address of the server
	 * @param port the port of the server
	 * @param timeoutMillis the timeout for the connection in millis, or a non-positive value for no timeout
	 * @return true if the connection was successful
	 * @throws InterruptedException if the thread gets interrupted while connecting
	 */
Connect to the server synchronously. Once it is completed, the client is ready to send and receive data.
Connect to the server synchronously . Once it is completed the client is ready to send and receive data .
public boolean connect(String host, int port, long timeoutMillis) throws InterruptedException {
		if (!initialized.getAndSet(true)) {
			bootstrap = new Bootstrap();
			workerGroup = new NioEventLoopGroup();
			bootstrap.group(workerGroup)
					.channel(NioSocketChannel.class)
					.handler(new ChannelInitializer<SocketChannel>() {
						@Override
						public void initChannel(SocketChannel channel) {
							channel.pipeline().addLast(new PacketDecoder<>(serializer),
									new LengthFieldPrepender(4),
									new PacketEncoder<>(serializer),
									new ClientChannelHandler());
							Consumer<SocketChannel> consumer = onChannelInitialized;
							if (consumer != null) {
								consumer.accept(channel);
							}
						}
					})
					.option(ChannelOption.SO_KEEPALIVE, true);
			Consumer<Bootstrap> consumer = onConfigured;
			if (consumer != null) {
				consumer.accept(bootstrap);
			}
		}
		ChannelFuture future = bootstrap.connect(host, port);
		if (timeoutMillis <= 0) {
			future.sync();
			return true;
		}
		boolean inTime = future.await(timeoutMillis);
		if (future.cause() != null) {
			System.out.println("Exception...");
			PlatformDependent.throwException(future.cause());
		}
		return inTime;
	}
public boolean connect ( String host , int port , long timeoutMillis ) throws InterruptedException { if ( ! initialized . getAndSet ( true ) ) { bootstrap = new Bootstrap ( ) ; workerGroup = new NioEventLoopGroup ( ) ; bootstrap . group ( workerGroup ) . channel ( NioSocketChannel . class ) . handler ( new ChannelInitializer < SocketChannel > ( ) { @ Override public void initChannel ( SocketChannel channel ) { channel . pipeline ( ) . addLast ( new PacketDecoder < > ( serializer ) , new LengthFieldPrepender ( 4 ) , new PacketEncoder < > ( serializer ) , new ClientChannelHandler ( ) ) ; Consumer < SocketChannel > consumer = onChannelInitialized ; if ( consumer != null ) { consumer . accept ( channel ) ; } } } ) . option ( ChannelOption . SO_KEEPALIVE , true ) ; Consumer < Bootstrap > consumer = onConfigured ; if ( consumer != null ) { consumer . accept ( bootstrap ) ; } } ChannelFuture future = bootstrap . connect ( host , port ) ; if ( timeoutMillis <= 0 ) { future . sync ( ) ; return true ; } boolean inTime = future . await ( timeoutMillis ) ; if ( future . cause ( ) != null ) { System . out . println ( "Exception..." ) ; PlatformDependent . throwException ( future . cause ( ) ) ; } return inTime ; }
Connect to the server synchronously.
Connect to the server synchronously .