View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.example.telnet;
17  
18  import java.io.BufferedReader;
19  import java.io.IOException;
20  import java.io.InputStreamReader;
21  import java.net.InetSocketAddress;
22  import java.util.concurrent.Executors;
23  
24  import org.jboss.netty.bootstrap.ClientBootstrap;
25  import org.jboss.netty.channel.Channel;
26  import org.jboss.netty.channel.ChannelFuture;
27  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
28  
29  /**
30   * Simplistic telnet client.
31   */
32  public class TelnetClient {
33  
34      private final String host;
35      private final int port;
36  
37      public TelnetClient(String host, int port) {
38          this.host = host;
39          this.port = port;
40      }
41  
42      public void run() throws IOException {
43          // Configure the client.
44          ClientBootstrap bootstrap = new ClientBootstrap(
45                  new NioClientSocketChannelFactory(
46                          Executors.newCachedThreadPool(),
47                          Executors.newCachedThreadPool()));
48  
49          // Configure the pipeline factory.
50          bootstrap.setPipelineFactory(new TelnetClientPipelineFactory());
51  
52          // Start the connection attempt.
53          ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
54  
55          // Wait until the connection attempt succeeds or fails.
56          Channel channel = future.awaitUninterruptibly().getChannel();
57          if (!future.isSuccess()) {
58              future.getCause().printStackTrace();
59              bootstrap.releaseExternalResources();
60              return;
61          }
62  
63          // Read commands from the stdin.
64          ChannelFuture lastWriteFuture = null;
65          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
66          for (;;) {
67              String line = in.readLine();
68              if (line == null) {
69                  break;
70              }
71  
72              // Sends the received line to the server.
73              lastWriteFuture = channel.write(line + "\r\n");
74  
75              // If user typed the 'bye' command, wait until the server closes
76              // the connection.
77              if (line.toLowerCase().equals("bye")) {
78                  channel.getCloseFuture().awaitUninterruptibly();
79                  break;
80              }
81          }
82  
83          // Wait until all messages are flushed before closing the channel.
84          if (lastWriteFuture != null) {
85              lastWriteFuture.awaitUninterruptibly();
86          }
87  
88          // Close the connection.  Make sure the close operation ends because
89          // all I/O operations are asynchronous in Netty.
90          channel.close().awaitUninterruptibly();
91  
92          // Shut down all thread pools to exit.
93          bootstrap.releaseExternalResources();
94      }
95  
96      public static void main(String[] args) throws Exception {
97          // Print usage if no argument is specified.
98          if (args.length != 2) {
99              System.err.println(
100                     "Usage: " + TelnetClient.class.getSimpleName() +
101                     " <host> <port>");
102             return;
103         }
104 
105         // Parse options.
106         String host = args[0];
107         int port = Integer.parseInt(args[1]);
108 
109         new TelnetClient(host, port).run();
110     }
111 }