通过unix域套接字支持netty for datagram数据包?

时间:2016-04-22 18:05:14

标签: sockets unix netty

我刚开始查看某些项目的netty,并且能够运行一些使用INET和unix域套接字来回传送消息的简单客户端和服务器示例。我也能够通过INET套接字发送数据报包。但我需要通过UNIX域套接字发送数据报包。这是否支持netty?如果是这样,有人可以指点我的文档或示例吗?我怀疑这是不受支持的,因为DatagramPacket明确地采用了InetSocketAddress。如果不支持,是否可以将其添加到netty?

1 个答案:

答案 0 :(得分:-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();
    }
}