java網路編程參考文章

來源:互聯網
上載者:User

標籤:

轉自:http://www.cnblogs.com/wucao/p/3934913.html

MINA、Netty、Twisted為什麼放在一起學習?首先,不妨先分別看一下它們官方網站對其的介紹:

MINA:

Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract event-driven asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.

Netty:

Netty is an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients.

Twisted:

Twisted is an event-driven networking engine written in Python and licensed under the open source MIT license.

(Twisted官網的文案不專業啊,居然不寫asynchronous)

從上面簡短的介紹中,就可以發現它們的共同特點:event-driven以及asynchronous。它們都是事件驅動、非同步網路編程架構。由此可見,它們之間的共同點還是很明顯的。所以我這裡將這三個架構放在一起,實現相同的功能,不但可以用少量的精力學三樣東西,而且還可以對它們之間進行各方面的對比。

其中MINA和Netty是基於Java語言的,而Twisted是Python語言的。不過語言不是重點,重點的是理念。

使用傳統的BIO(Blocking IO/阻塞IO)進行網路編程時,進行網路IO讀寫時都會阻塞當前線程,如果實現一個TCP伺服器,都需要對每個用戶端串連開啟一個線程,而很多線程可能都在傻傻的阻塞住等待讀寫資料,系統資源消耗大。

而NIO(Non-Blocking IO/非阻塞IO)或AIO(Asynchronous IO/非同步IO)則是通過IO多工技術實現,不需要為每個串連建立一個線程,其底層實現是通過作業系統的一些特性如select、poll、epoll、iocp等。這三個網路架構都是基於此實現。

下面分別用這三個架構實現一個最簡單的TCP伺服器。當接收到用戶端發過來的字串後,向用戶端回寫一個字串作為響應。

Mina:

public class TcpServer {        public static void main(String[] args) throws IOException {          IoAcceptor acceptor = new NioSocketAcceptor();          acceptor.setHandler(new TcpServerHandle());          acceptor.bind(new InetSocketAddress(8080));      }    }    class TcpServerHandle extends IoHandlerAdapter {            @Override      public void exceptionCaught(IoSession session, Throwable cause) throws Exception {          cause.printStackTrace();      }        // 接收到新的資料      @Override      public void messageReceived(IoSession session, Object message) throws Exception {                    // 接收用戶端的資料          IoBuffer ioBuffer = (IoBuffer) message;          byte[] byteArray = new byte[ioBuffer.limit()];          ioBuffer.get(byteArray, 0, ioBuffer.limit());          System.out.println("messageReceived:" + new String(byteArray, "UTF-8"));                    // 發送到用戶端          byte[] responseByteArray = "你好".getBytes("UTF-8");          IoBuffer responseIoBuffer = IoBuffer.allocate(responseByteArray.length);          responseIoBuffer.put(responseByteArray);          responseIoBuffer.flip();          session.write(responseIoBuffer);      }        @Override      public void sessionCreated(IoSession session) throws Exception {          System.out.println("sessionCreated");      }            @Override      public void sessionClosed(IoSession session) throws Exception {          System.out.println("sessionClosed");      }  }  

Netty:

public class TcpServer {        public static void main(String[] args) throws InterruptedException {          EventLoopGroup bossGroup = new NioEventLoopGroup();          EventLoopGroup workerGroup = new NioEventLoopGroup();          try {              ServerBootstrap b = new ServerBootstrap();              b.group(bossGroup, workerGroup)                      .channel(NioServerSocketChannel.class)                      .childHandler(new ChannelInitializer<SocketChannel>() {                          @Override                          public void initChannel(SocketChannel ch)                                  throws Exception {                              ch.pipeline().addLast(new TcpServerHandler());                          }                      });              ChannelFuture f = b.bind(8080).sync();              f.channel().closeFuture().sync();          } finally {              workerGroup.shutdownGracefully();              bossGroup.shutdownGracefully();          }      }    }    class TcpServerHandler extends ChannelInboundHandlerAdapter {        // 接收到新的資料      @Override      public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException {          try {              // 接收用戶端的資料              ByteBuf in = (ByteBuf) msg;              System.out.println("channelRead:" + in.toString(CharsetUtil.UTF_8));                            // 發送到用戶端              byte[] responseByteArray = "你好".getBytes("UTF-8");              ByteBuf out = ctx.alloc().buffer(responseByteArray.length);              out.writeBytes(responseByteArray);              ctx.writeAndFlush(out);                        } finally {              ReferenceCountUtil.release(msg);          }      }            @Override      public void channelActive(ChannelHandlerContext ctx) {          System.out.println("channelActive");      }            @Override      public void channelInactive(ChannelHandlerContext ctx){          System.out.println("channelInactive");      }        @Override      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {          cause.printStackTrace();          ctx.close();      }  }  

Twisted:

# -*- coding:utf-8 –*-    from twisted.internet.protocol import Protocol  from twisted.internet.protocol import Factory  from twisted.internet import reactor    class TcpServerHandle(Protocol):            # 新的串連建立      def connectionMade(self):          print ‘connectionMade‘                # 串連斷開      def connectionLost(self, reason):          print ‘connectionLost‘            # 接收到新資料      def dataReceived(self, data):          print ‘dataReceived‘, data          self.transport.write(‘你好‘)    factory = Factory()  factory.protocol = TcpServerHandle  reactor.listenTCP(8080, factory)  reactor.run() 

上面的代碼可以看出,這三個架構實現的TCP伺服器,在串連建立、接收到用戶端傳來的資料、串連關閉時,都會觸發某個事件。例如接收到用戶端傳來的資料時,MINA會觸發事件調用messageReceived,Netty會調用channelRead,Twisted會調用dataReceived。編寫代碼時,只需要繼承一個類並重寫響應的方法即可。這就是event-driven事件驅動。

下面是Java寫的一個TCP用戶端用作測試,用戶端沒有使用這三個架構,也沒有使用NIO,只是一個普通的BIO的TCP用戶端。

TCP在建立串連到關閉串連的過程中,可以多次進行發送和接收資料。下面的用戶端發送了兩個字串到伺服器並兩次擷取伺服器回應的資料,之間通過Thread.sleep(5000)間隔5秒。

public class TcpClient {            public static void main(String[] args) throws IOException, InterruptedException {                              Socket socket = null;          OutputStream out = null;          InputStream in = null;                    try{                            socket = new Socket("localhost", 8080);                    out = socket.getOutputStream();              in = socket.getInputStream();                            // 請求伺服器              out.write("第一次請求".getBytes("UTF-8"));              out.flush();                                    // 擷取伺服器響應,輸出              byte[] byteArray = new byte[1024];              int length = in.read(byteArray);              System.out.println(new String(byteArray, 0, length, "UTF-8"));                            Thread.sleep(5000);                            // 再次請求伺服器              out.write("第二次請求".getBytes("UTF-8"));              out.flush();                            // 再次擷取伺服器響應,輸出              byteArray = new byte[1024];              length = in.read(byteArray);              System.out.println(new String(byteArray, 0, length, "UTF-8"));                                      } finally {              // 關閉串連              in.close();              out.close();              socket.close();          }      }  }  

用用戶端分別測試上面三個TCP伺服器:

MINA伺服器輸出結果:

sessionCreated
messageReceived:第一次請求
messageReceived:第二次請求
sessionClosed

Netty伺服器輸出結果:

channelActive
channelRead:第一次請求
channelRead:第二次請求
channelInactive

Twisted伺服器輸出結果:

connectionMade
dataReceived: 第一次請求
dataReceived: 第二次請求
connectionLost

java網路編程參考文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.