View Javadoc

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
    10   *    http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   *  Unless required by applicable law or agreed to in writing,
    13   *  software distributed under the License is distributed on an
    14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   *  KIND, either express or implied.  See the License for the
    16   *  specific language governing permissions and limitations
    17   *  under the License.
    18   *
    19   */
    20
    21  package org.apache.mina.examples.echoclient;
    22
    23  import java.net.InetSocketAddress;
    24  import java.nio.ByteBuffer;
    25  import java.util.concurrent.ExecutionException;
    26
    27  import org.apache.mina.api.AbstractIoHandler;
    28  import org.apache.mina.api.IoFuture;
    29  import org.apache.mina.api.IoSession;
    30  import org.apache.mina.examples.echoserver.NioEchoServer;
    31  import org.apache.mina.transport.nio.NioTcpClient;
    32  import org.slf4j.Logger;
    33  import org.slf4j.LoggerFactory;
    34
    35  /**
    36   * A simple TCP client, write back to the client every received messages.
    37   *
    38   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
    39   *
    40   */
    41  public class NioEchoClient {
    42
    43      static final private Logger LOG = LoggerFactory.getLogger(NioEchoServer.class);
    44
    45      public static void main(String[] args) {
    46          LOG.info("starting echo client");
    47
    48          final NioTcpClient client = new NioTcpClient();
    49          client.setFilters();
    50          client.setIoHandler(new AbstractIoHandler() {
    51              @Override
    52              public void sessionOpened(final IoSession session) {
    53                  LOG.info("session opened {}", session);
    54              }
    55
    56              @Override
    57              public void messageReceived(IoSession session, Object message) {
    58                  LOG.info("message received {}", message);
    59                  if (message instanceof ByteBuffer) {
    60                      LOG.info("echoing");
    61                      session.write(message);
    62                  }
    63              }
    64
    65              @Override
    66              public void messageSent(IoSession session, Object message) {
    67                  LOG.info("message sent {}", message);
    68              }
    69
    70              @Override
    71              public void sessionClosed(IoSession session) {
    72                  LOG.info("session closed {}", session);
    73              }
    74          });
    75
    76          try {
    77              IoFuture<IoSession> future = client.connect(new InetSocketAddress("localhost", 9999));
    78
    79              try {
    80                  IoSession session = future.get();
    81                  LOG.info("session connected : {}", session);
    82              } catch (ExecutionException e) {
    83                  LOG.error("cannot connect : ", e);
    84              }
    85
    86              LOG.debug("Running the client for 25 sec");
    87              Thread.sleep(25000);
    88          } catch (InterruptedException e) {
    89          }
    90      }
    91  }