⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 mina是以Java实现的一个开源的网络程序框架
💻 JAVA
字号:
/* *  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. * */package org.apache.mina.example.sumup;import java.net.InetSocketAddress;import org.apache.mina.core.RuntimeIoException;import org.apache.mina.core.future.ConnectFuture;import org.apache.mina.core.session.IoSession;import org.apache.mina.example.sumup.codec.SumUpProtocolCodecFactory;import org.apache.mina.filter.codec.ProtocolCodecFilter;import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;import org.apache.mina.filter.logging.LoggingFilter;import org.apache.mina.transport.socket.nio.NioSocketConnector;/** * (<strong>Entry Point</strong>) Starts SumUp client. * * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (Thu, 26 Jun 2008) $ */public class Client {    private static final String HOSTNAME = "localhost";    private static final int PORT = 8080;    private static final long CONNECT_TIMEOUT = 30*1000L; // 30 seconds    // Set this to false to use object serialization instead of custom codec.    private static final boolean USE_CUSTOM_CODEC = true;    public static void main(String[] args) throws Throwable {        if (args.length == 0) {            System.out.println("Please specify the list of any integers");            return;        }        // prepare values to sum up        int[] values = new int[args.length];        for (int i = 0; i < args.length; i++) {            values[i] = Integer.parseInt(args[i]);        }        NioSocketConnector connector = new NioSocketConnector();        // Configure the service.        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();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -