2016-04-22 135 views
2

我刚刚开始关注一些项目的netty,并且已经能够获得一些简单的客户端和服务器示例,它们运行使用INET和unix域套接字来回发送消息。我也能够通过INET套接字发送数据报包。但是我需要通过UNIX域套接字发送数据报包。这是在netty中支持的吗?如果是这样,有人可以指向我的文档或例子吗?我怀疑这不支持,因为DatagramPacket显式地使用InetSocketAddress。如果不支持,将它添加到netty是否可行?通过unix域套接字支持netty数据包数据包?

回答

-1

这是netty支持吗?

是的。以下是我写的一个简单例子。

import io.netty.bootstrap.Bootstrap; 
import io.netty.bootstrap.ServerBootstrap; 
import io.netty.buffer.ByteBuf; 
import io.netty.channel.*; 
import io.netty.channel.epoll.EpollDomainSocketChannel; 
import io.netty.channel.epoll.EpollEventLoopGroup; 
import io.netty.channel.epoll.EpollServerDomainSocketChannel; 
import io.netty.channel.unix.DomainSocketAddress; 

/** 
* @author louyl 
*/ 
public class App { 
    public static void main(String[] args) throws Exception { 
     String sockPath = "/tmp/echo.sock"; 
     final ServerBootstrap bootstrap = new ServerBootstrap(); 
     EventLoopGroup serverBossEventLoopGroup = new EpollEventLoopGroup(); 
     EventLoopGroup serverWorkerEventLoopGroup = new EpollEventLoopGroup(); 
     bootstrap.group(serverBossEventLoopGroup, serverWorkerEventLoopGroup) 
      .localAddress(new DomainSocketAddress(sockPath)) 
      .channel(EpollServerDomainSocketChannel.class) 
      .childHandler(
       new ChannelInitializer<Channel>() { 
        @Override 
        protected void initChannel(final Channel channel) throws Exception { 
         channel.pipeline().addLast(
          new ChannelInboundHandlerAdapter() { 
           @Override 
           public void channelActive(final ChannelHandlerContext ctx) throws Exception { 
            final ByteBuf buff = ctx.alloc().buffer(); 
            buff.writeBytes("This is a test".getBytes()); 
            ctx.writeAndFlush(buff).addListeners(new ChannelFutureListener() { 
              @Override 
              public void operationComplete(ChannelFuture future) { 
               future.channel().close(); 
               future.channel().parent().close(); 
              } 
             }); 
           } 
          } 
                ); 
        } 
       } 
         ); 
     final ChannelFuture serverFuture = bootstrap.bind().sync(); 

     final Bootstrap bootstrapClient = new Bootstrap(); 
     EventLoopGroup clientEventLoop = new EpollEventLoopGroup(); 
     bootstrapClient.group(clientEventLoop) 
      .channel(EpollDomainSocketChannel.class) 
      .handler(new ChannelInitializer<Channel>() { 
        @Override 
        protected void initChannel(final Channel channel) throws Exception { 
         channel.pipeline().addLast(
          new ChannelInboundHandlerAdapter() { 
           @Override 
           public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception { 
            final ByteBuf buff = (ByteBuf) msg; 
            try { 
             byte[] bytes = new byte[buff.readableBytes()]; 
             buff.getBytes(0, bytes); 
             System.out.println(new String(bytes)); 
            } finally { 
             buff.release(); 
            } 
            ctx.close(); 
           } 

           @Override 
           public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) throws Exception { 
            System.out.println("Error occur when reading from Unix domain socket: " + cause.getMessage()); 
            ctx.close(); 
           } 
          } 
                ); 
        } 
       } 
       ); 
     final ChannelFuture clientFuture = bootstrapClient.connect(new DomainSocketAddress(sockPath)).sync(); 

     clientFuture.channel().closeFuture().sync(); 
     serverFuture.channel().closeFuture().sync(); 
     serverBossEventLoopGroup.shutdownGracefully(); 
     serverWorkerEventLoopGroup.shutdownGracefully(); 
     clientEventLoop.shutdownGracefully(); 
    } 
}