`
phantom
  • 浏览: 162452 次
社区版块
存档分类
最新评论

Mina 应用之——代理服务器

阅读更多
/*
 *  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 a.proxy.test;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import org.apache.mina.core.RuntimeIoException;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.IoFutureListener;
import org.apache.mina.core.service.IoConnector;
import org.apache.mina.core.session.IoSession;


public class ClientToProxyIoHandler extends AbstractProxyIoHandler {
    private final ServerToProxyIoHandler connectorHandler = new ServerToProxyIoHandler();

    private final IoConnector connector;

    private final SocketAddress[] remoteAddress;

    public ClientToProxyIoHandler(IoConnector connector,
            SocketAddress[] remoteAddress) {
        this.connector = connector;
        this.remoteAddress = remoteAddress;
        connector.setHandler(connectorHandler);
    }
    InetSocketAddress isas[]=new InetSocketAddress[]{
    		new InetSocketAddress("113.31.18.232", 22),
    		new InetSocketAddress("113.31.18.233", 22),
    		new InetSocketAddress("113.31.18.234", 22),
    		new InetSocketAddress("113.31.18.235", 22),
    		new InetSocketAddress("113.31.18.236", 22),
    	
    		
    };
    int k=0;
    @Override
    public void sessionOpened(final IoSession session) throws Exception {
       
    
        //isa[(k++)%isa.length]
        SocketAddress isa= remoteAddress[(k++)%remoteAddress.length];
        System.out.println("#$#$"+isa);
      
        connector.connect(isa).addListener(new IoFutureListener<ConnectFuture>() {
            public void operationComplete(ConnectFuture future) {
              
                try {
                    future.getSession().setAttribute(OTHER_IO_SESSION, session);
                    session.setAttribute(OTHER_IO_SESSION, future.getSession());
                    IoSession session2 = future.getSession();
                    session2.resumeRead();
                    session2.resumeWrite();
                } catch (RuntimeIoException e) {
                    // Connect failed
                    session.close(true);
                } finally {
                    session.resumeRead();
                    session.resumeWrite();
                 
                }
            }
        });
    }
}




/*
 *  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 a.proxy.test;

import java.nio.charset.Charset;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;


public abstract class AbstractProxyIoHandler extends IoHandlerAdapter {
    private static final Charset CHARSET = Charset.forName("iso8859-1");
    public static final String OTHER_IO_SESSION = AbstractProxyIoHandler.class.getName()+".OtherIoSession";

  
    @Override
    public void sessionCreated(IoSession session) throws Exception {
        session.suspendRead();
        session.suspendWrite();
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        if (session.getAttribute( OTHER_IO_SESSION ) != null) {
            IoSession sess = (IoSession) session.getAttribute(OTHER_IO_SESSION);
            sess.setAttribute(OTHER_IO_SESSION, null);
            sess.close(false);
            session.setAttribute(OTHER_IO_SESSION, null);
        }
    }

    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {
        IoBuffer rb = (IoBuffer) message;
        IoBuffer wb = IoBuffer.allocate(rb.remaining());
        rb.mark();
        wb.put(rb);
        wb.flip();
        ((IoSession) session.getAttribute(OTHER_IO_SESSION)).write(wb);
        rb.reset();
    
    }
}



/*
 *  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 a.proxy.test;

import java.net.InetSocketAddress;

import org.apache.mina.core.service.IoConnector;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketConnector;
public class Main {
    public static void main(String[] args2) throws Exception {
        // Create TCP/IP acceptor.
        NioSocketAcceptor acceptor = new NioSocketAcceptor();
        IoConnector connector = new NioSocketConnector();
        connector.setConnectTimeoutMillis(30*1000L);
        ClientToProxyIoHandler handler = new ClientToProxyIoHandler(connector,	new InetSocketAddress[]{ new InetSocketAddress("127.0.0.1", 80)});
        acceptor.setHandler(handler);
        acceptor.bind(new InetSocketAddress(777));
        System.out.println("Listening on port " + 777);
    }


}

分享到:
评论

相关推荐

    Mina官网例子之时间服务器

    **Mina官网例子之时间服务器** Apache Mina(Minimum Asynchronous Network)是一个高度可扩展的网络通信框架,它为各种协议提供了低级别的基础结构,包括TCP/IP和UDP/IP。Mina的目标是简化网络编程,使其变得高效...

    Mina开发之服务器

    Mina开发之服务器的代码,详情请查看:http://www.cnblogs.com/getherBlog/p/3937196.html Mina开发之客户端的代码,详情请查看:http://www.cnblogs.com/getherBlog/p/3937196.html

    mina 服务器简介,个人感觉不错

    Apache Mina是一个高性能的网络应用框架,主要用于简化开发网络服务应用程序。Mina提供了一种抽象层,使得开发者能够专注于业务逻辑,而不是底层的网络通信细节。以下是对Mina服务器核心概念的详细解释: 1. **...

    mina服务器和客服端实现

    《mina服务器和客户端实现详解》 Apache Mina(Minimum Asynchronous Network)是一个高度可扩展的网络通信框架,它为开发者提供了构建高性能、高可用性的网络应用程序的基础。在本文中,我们将深入探讨如何利用...

    一种基于Mina状态机的高性能服务器的设计方法

    基于 Mina 状态机的服务器设计方法通过实现状态机,编写自定义编解码器,并在 Mina 框架中创建状态机对象,设置 I/O 代理的过程,编制了基于 Mina 状态机的五子棋游戏服务器程序。在模拟环境下,测试结果表明,该...

    websocket+java服务器(mina)

    Mina(Java Multithreaded Network Application Framework)是一个用Java编写的网络应用框架,它提供了高度可扩展性和性能,适用于多种网络协议,包括TCP和UDP。Mina为开发者提供了一种抽象层,简化了网络编程的复杂...

    Mina客户端服务器Demo

    **Mina客户端服务器Demo**是基于Apache Mina框架的一个示例项目,主要展示了如何使用Mina进行网络通信的开发。Apache Mina是一个高度可扩展且轻量级的Java框架,主要用于构建高性能、高效率的网络应用程序,如TCP/IP...

    mina 应用

    在实际应用中,MINA常用于构建TCP和UDP服务器,例如,它可以用于实现FTP服务器、HTTP服务器、聊天应用服务器等。MINA的灵活性和高效性使其在高并发、低延迟的场景下表现出色。 通过阅读链接中的博客文章《navylee....

    mina客户端服务器简易Demo

    总的来说,Apache Mina是一个强大的工具,通过学习和实践这个"mina客户端服务器简易Demo",你可以快速掌握其基本用法,并以此为基础构建更复杂的网络应用。在深入研究Mina的过程中,你将了解到如何处理网络连接、...

    MINA 服务器端实现 可运行

    MINA(Multipurpose Infrastructure for Network Applications)是一个高性能、异步事件驱动的网络应用程序框架,用于构建高并发、低延迟的服务端应用。这个框架基于Java语言,适用于开发TCP、UDP等网络协议的应用,...

    Ruby-Mina快速部署和服务器的自动化工具

    Ruby-Mina是一款强大的服务器自动化和应用部署工具,尤其在Ruby开发环境中被广泛使用。它基于Rake,一个类似于Make的脚本语言,用于构建复杂的任务集合。Mina以其高效、轻量级和易于理解的特性,深受开发者喜爱,...

    Apache MINA基于JAVA的网络服务器框架 教程.zip

    Apache MINA(Multipurpose Infrastructure for Network Applications)是一个高性能、异步事件驱动的网络应用程序框架,主要用Java语言...MINA的灵活性和强大功能使其成为Java开发者构建复杂网络应用的首选框架之一。

    mina作为服务器的用法

    Apache Mina是一个开源框架,主要用于构建高性能、高可用性的网络应用程序。它主要关注网络通信,提供了基于事件驱动、异步I/O的模型,这使得它...通过理解上述知识点,你将能够熟练地运用Mina来构建自己的服务器应用。

    socket通讯和mina应用

    Socket通讯和MINA应用是Java网络编程中的两个关键概念,它们在开发分布式系统、网络服务和客户端应用程序中扮演着重要角色。这篇博文将深入探讨这两个主题,并通过一个名为"testMina"的压缩包文件来展示实际应用。 ...

    Mina开发之客户端

    Apache Mina(Minimum Asynchronous Network)是一个高度可扩展的、高性能的网络应用框架,主要用于构建服务器端的网络应用程序。它简化了网络编程的复杂性,提供了基于事件驱动和异步I/O模型的API。在本文中,我们...

    mina2.0.7实例(服务器端与客户端)

    服务器端模拟SFS2X的一些实用功能对mina框架作了一定的封装,使用起来己经和SFS2X没有 太多的区别,但客户端只能使用mina组件(也就是说只能是JAVA客户端,这个实在有点麻烦,一直 想应用在unity3d中,没有实现,不懂...

    Mina入门:mina版之HelloWorld

    在这个“Mina入门:Mina版之HelloWorld”教程中,我们将探讨如何使用Mina构建一个简单的网络通信应用。 首先,我们需要了解Mina的核心概念。Mina基于事件驱动和非阻塞I/O模型,这使得它在处理大量并发连接时表现...

    Mina通信框架应用示例

    - 客户端-服务器框架库:MINA支持多种通信协议,可以构建客户端和服务器端应用。 - 网络socket库:MINA提供了一套完整的网络通信接口,方便开发者进行数据传输操作。 2. Mina简单入门 开发MINA应用通常涉及以下...

    MINA_API+MINA_DOC+mina

    MINA (Java IO Network Application Framework) 是一个由Apache软件基金会开发的开源网络通信框架,主要应用于构建高性能、高可用性的网络服务器。这个压缩包包含了MINA API文档、自学手册以及开发指南,对于学习和...

Global site tag (gtag.js) - Google Analytics