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;
/**
* (<b>Entry point</b>) Demonstrates how to write a very simple tunneling proxy
* using MINA. The proxy only logs all data passing through it. This is only
* suitable for text based protocols since received data will be converted into
* strings before being logged.
* <p>
* Start a proxy like this:<br/>
* <code>org.apache.mina.example.proxy.Main 12345 www.google.com 80</code><br/>
* and open <a href="http://localhost:12345">http://localhost:12345</a> in a
* browser window.
* </p>
*
* @author The Apache MINA Project (dev@mina.apache.org)
* @version $Rev$, $Date$
*/
public class Main {
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println(Main.class.getName()
+ " <proxy-port> <server-hostname> <server-port>");
return;
}
// Create TCP/IP acceptor.
NioSocketAcceptor acceptor = new NioSocketAcceptor();
// Create TCP/IP connector.
IoConnector connector = new NioSocketConnector();
// Set connect timeout.
connector.setConnectTimeoutMillis(30*1000L);
ClientToProxyIoHandler handler = new ClientToProxyIoHandler(connector,
new InetSocketAddress(args[1], Integer.parseInt(args[2])));
// Start proxy.
acceptor.setHandler(handler);
acceptor.bind(new InetSocketAddress(Integer.parseInt(args[0])));
System.out.println("Listening on port " + Integer.parseInt(args[0]));
}
}
/**
* Handles the server to proxy part of the proxied connection.
*
* @author The Apache MINA Project (dev@mina.apache.org)
* @version $Rev$, $Date$
*
*/
public class ServerToProxyIoHandler extends AbstractProxyIoHandler {
}
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Base class of {@link org.apache.mina.core.service.IoHandler} classes which handle
* proxied connections.
*
* @author The Apache MINA Project (dev@mina.apache.org)
* @version $Rev$, $Date$
*
*/
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";
private final Logger logger = LoggerFactory.getLogger(getClass());
@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();
logger.info(rb.getString(CHARSET.newDecoder()));
}
}
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;
/**
* Handles the client to proxy part of the proxied connection.
*
* @author The Apache MINA Project (dev@mina.apache.org)
* @version $Rev$, $Date$
*
*/
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);
}
@Override
public void sessionOpened(final IoSession session) throws Exception {
connector.connect(remoteAddress).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();
}
}
});
}
}
分享到:
相关推荐
Mina开发之服务器的代码,详情请查看:http://www.cnblogs.com/getherBlog/p/3937196.html Mina开发之客户端的代码,详情请查看:http://www.cnblogs.com/getherBlog/p/3937196.html
mina的使用初步入门mina的使用初步入门mina的使用初步入门
Mina官网的时间服务例子,附有运行说明,完全能够运行。
mina 服务器简介,个人感觉不错, 感兴趣的可以下来看看
mina例子,服务端和客户端,MinaTimeServer是启动服务端,然后运行MinaClient就能看到结果了
这是个使用mina的例子,需要的可以下来看看
里面包括了 一个mina作为服务器的例子已经各种学习文档和学习笔记,方便刚入门的学习,自己也是被坑了好久,才学会使用
使用mina实现rpc调用.使用参考http://blog.csdn.net/stevexk/archive/2008/07/23/2697907.aspx
apache mina性能测试实例 四台客户端机器,服务器能跑到1w。mina的jar包自己去下,我上传的其他资源里也有。
mina 客户端与服务器端之间的通信,整个压缩包包括服务端与Android客户端的完整代码,下载解压导入即可运行喔。。。
服务器端模拟SFS2X的一些实用功能对mina框架作了一定的封装,使用起来己经和SFS2X没有 太多的区别,但客户端只能使用mina组件(也就是说只能是JAVA客户端,这个实在有点麻烦,一直 想应用在unity3d中,没有实现,不懂...
Mina网络通信开发Demo,Mina客户端服务器Demo,很好的代码学习资料
服务器框架MINA使用经验总结 socket协议通信框架
NULL 博文链接:https://wuzijingaip.iteye.com/blog/1266315
请与“MINA_Client_Test”一起使用,可运行
mina服务器和客服端实现,包含编解码,等一系列的代码。
最近做rfid读写,C#和java都用udp不用厂家的动态库,udp自己写也简单,但是试了一下Apache mina ,接收的不是string,二十byte[] 数组,简单实现了UDP,网上也有例子,但是不是我要的。可用。
java mina 消息推送
mina服务端工程例子包括相关源码,实现了socket 的nio
简单实现mina服务器框架的websocket通信(Simple implementation of websocket communication in mina server