Java tutorial
/* * Copyright (c) 2013 Maxim Yunusov * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.maxur.clitelnetclient; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; final class Sender { public static final String EOL = "\r\n"; private final String host; private final int port; private final String command; Sender(final String host, final int port, final String command) { this.host = host; this.port = port; this.command = command; } void send() throws InterruptedException { final EventLoopGroup group = new NioEventLoopGroup(); try { final Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class).handler(new TelnetClientInitializer()); final Channel channel = bootstrap.connect(host, port).sync().channel(); final ChannelFuture lastWriteFuture = channel.writeAndFlush(command + EOL); channel.closeFuture().sync(); if (lastWriteFuture != null) { lastWriteFuture.sync(); } } finally { group.shutdownGracefully(); } } }