2

我们在当前项目中使用了用于 TCP/IP 协议的 Apache MINA,其中服务器和客户端通过 TCP/IP 进行通信。所以我们已经有了一些 MINA 的代码。

现在我们也想支持 http 协议,并尽可能重用。

我需要一些客户端/服务器代码示例,用于使用 MINA-HTTP 的 URL (/servlet) 进行连接。

我找到了如下链接- http://mina.apache.org/report/trunk/xref/org/apache/mina/example/httpserver/codec/package-frame.html

但正在寻找服务器和客户端的示例代码。如果有人可以帮助我,那就太好了

谢谢,

4

2 回答 2

1
   NioSocketAcceptor acceptor = new NioSocketAcceptor();
   acceptor.getFilterChain().addLast( "codec", new  HttpServerCodec());
   acceptor.setHandler(new IoHandlerAdapter() { 
    public void messageReceived(IoSession session, Object message)throws Exception {
         if (message instanceof HttpRequestImpl) {
            HttpRequestImpl request1 = (HttpRequestImpl) message;

        }
        if (message instanceof IoBuffer) {
            SocketAddress addr = session.getRemoteAddress()
            IoBuffer simbuf = (IoBuffer) message;
            byte[] buff = simbuf.array();
            String decoded = new String(buff, "UTF-8");
            System.out.println(decoded);

        }
        if (message instanceof HttpEndOfContent) {
            byte[] bytes = "OK".getBytes();
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Length", Integer.toString(bytes.length));
            DefaultHttpResponse resp = new DefaultHttpResponse(
                    HttpVersion.HTTP_1_1, HttpStatus.SUCCESS_OK, headers);
            session.write(resp);
            session.write(IoBuffer.wrap(bytes));
            session.write(new HttpEndOfContent());

        }

    } 
});
 try {
        acceptor.bind( new InetSocketAddress(1234) );
    } catch (IOException e) {
        e.printStackTrace();
    }   
于 2014-07-10T05:17:00.080 回答
1
    final Object lock = new Object();
    IoAcceptor acceptor = new NioSocketAcceptor();
    ((NioSocketAcceptor) acceptor).setReuseAddress(true);
    acceptor.getFilterChain().addLast("httpServer", new HttpServerCodec());
    acceptor.setHandler(new IoHandlerAdapter() {
        @Override
        public void messageReceived(IoSession session, Object message) throws Exception {
            // System.out.println("message "+message.getClass()+" "+message);
            if (message instanceof HttpRequest) {
                byte[] bytes = "Hello World!".getBytes();
                Map<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Length", Integer.toString(bytes.length));
                DefaultHttpResponse resp = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SUCCESS_OK,
                        headers);
                session.write(resp);
                session.write(IoBuffer.wrap(bytes));
                session.write(new HttpEndOfContent());
                if (((HttpRequest) message).getRequestPath().equals("/close")) {
                    synchronized (lock) {
                        lock.notify();
                    }
                }
            }
        }
    });
    acceptor.bind(new InetSocketAddress(21212));
    synchronized (lock) {
        lock.wait();
    }
    acceptor.unbind();
    acceptor.dispose(true);
于 2012-12-28T20:22:24.633 回答