| Title: 2.3 - Sample TCP Client |
| NavPrev: ch2.2-sample-tcp-server.html |
| NavPrevText: 2.2 - Sample TCP Server |
| NavUp: ch2-basics.html |
| NavUpText: Chapter 2 - Basics |
| NavNext: ch2.4-sample-udp-server.html |
| NavNextText: 2.4 - Sample UDP Server |
| Notice: Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you 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. |
| |
| # 2.3 - Sample TCP Client |
| |
| We have seen the Client Architecture. Lets explore a sample Client implementation. |
| |
| We shall use [Sumup Client](http://mina.apache.org/mina-project/xref/org/apache/mina/example/sumup/Client.html) as a reference implementation. |
| |
| We will remove boiler plate code and concentrate on the important constructs. Below the code for the Client : |
| |
| :::java |
| public static void main(String[] args) throws Throwable { |
| NioSocketConnector connector = new NioSocketConnector(); |
| connector.setConnectTimeoutMillis(CONNECT_TIMEOUT); |
| |
| if (USE_CUSTOM_CODEC) { |
| connector.getFilterChain().addLast("codec", |
| new ProtocolCodecFilter(new SumUpProtocolCodecFactory(false))); |
| } else { |
| connector.getFilterChain().addLast("codec", |
| new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); |
| } |
| |
| connector.getFilterChain().addLast("logger", new LoggingFilter()); |
| connector.setHandler(new ClientSessionHandler(values)); |
| IoSession session; |
| |
| for (;;) { |
| try { |
| ConnectFuture future = connector.connect(new InetSocketAddress(HOSTNAME, PORT)); |
| future.awaitUninterruptibly(); |
| session = future.getSession(); |
| break; |
| } catch (RuntimeIoException e) { |
| System.err.println("Failed to connect."); |
| e.printStackTrace(); |
| Thread.sleep(5000); |
| } |
| } |
| |
| // wait until the summation is done |
| session.getCloseFuture().awaitUninterruptibly(); |
| connector.dispose(); |
| } |
| |
| To construct a Client, we need to do following |
| |
| * Create a Connector |
| * Create a Filter Chain |
| * Create a IOHandler and add to Connector |
| * Bind to Server |
| |
| Lets examine each one in detail |
| |
| ## Create a Connector |
| |
| :::java |
| NioSocketConnector connector = new NioSocketConnector(); |
| |
| Here we have created a NIO Socket connector |
| |
| ## Create a Filter Chain |
| |
| :::java |
| if (USE_CUSTOM_CODEC) { |
| connector.getFilterChain().addLast("codec", |
| new ProtocolCodecFilter(new SumUpProtocolCodecFactory(false))); |
| } else { |
| connector.getFilterChain().addLast("codec", |
| new ProtocolCodecFilter(new ObjectSerializationCodecFactory())); |
| } |
| |
| We add Filters to the Filter Chain for the Connector. Here we have added a ProtocolCodec, to the filter Chain. |
| |
| ## Create IOHandler |
| |
| :::java |
| connector.setHandler(new ClientSessionHandler(values)); |
| |
| Here we create an instance of [ClientSessionHandler](http://mina.apache.org/mina-project/xref/org/apache/mina/example/sumup/ClientSessionHandler.html) and set it as a handler for the Connector. |
| |
| ## Bind to Server |
| |
| :::java |
| IoSession session; |
| |
| for (;;) { |
| try { |
| ConnectFuture future = connector.connect(new InetSocketAddress(HOSTNAME, PORT)); |
| future.awaitUninterruptibly(); |
| session = future.getSession(); |
| break; |
| } catch (RuntimeIoException e) { |
| System.err.println("Failed to connect."); |
| e.printStackTrace(); |
| Thread.sleep(5000); |
| } |
| } |
| |
| Here is the most important stuff. We connect to remote Server. Since, connect is an async task, we use the [ConnectFuture](http://mina.apache.org/mina-project/xref/org/apache/mina/core/future/ConnectFuture.html) class to know the when the connection is complete. |
| Once the connection is complete, we get the associated [IoSession](http://mina.apache.org/mina-project/xref/org/apache/mina/core/session/IoSession.html). To send any message to the Server, we shall have to write to the session. All responses/messages from server shall traverse the Filter chain and finally be handled in IoHandler. |