Netty UDP协议通信

UDP其实不分Server与Client端,不过这里还是区分一下,尽管代码几乎是完全一样的....

(更多有关Netty的样例代码,见本人的Netty学习:https://github.com/xycodec/NettyLecture)

Server端:

package com.xycode.netty.udp;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.util.CharsetUtil;

public class UDPServer {
    public static void main(String[] args) {
        Bootstrap bootstrap=new Bootstrap();
        EventLoopGroup serverGroup=new NioEventLoopGroup();
        bootstrap
                .group(serverGroup)
                .channel(NioDatagramChannel.class)
                //.option(ChannelOption.SO_BROADCAST, true)
                .handler(
                        new ChannelInitializer<NioDatagramChannel>() {
                            @Override
                            protected void initChannel(NioDatagramChannel ch) throws Exception {
                                ChannelPipeline pipeline=ch.pipeline();
                                pipeline.addLast(new SimpleChannelInboundHandler<DatagramPacket>() {
                                    @Override
                                    protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
                                        System.out.println("[server] recv: "+msg.content().toString(CharsetUtil.UTF_8)+", from "+msg.sender());
                                        ctx.writeAndFlush(new DatagramPacket(
                                                Unpooled.copiedBuffer("this is a echo from server!".getBytes()),
                                                msg.sender()));
                                    }
                                });
                            }
                        }

                );

        ChannelFuture future=bootstrap.bind(2233);
        try {
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            serverGroup.shutdownGracefully().syncUninterruptibly();
        }


    }
}

Client端:

package com.xycode.netty.udp;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.util.CharsetUtil;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

public class UDPClient {
    public static void main(String[] args) {
        Bootstrap bootstrap=new Bootstrap();
        EventLoopGroup clientGroup=new NioEventLoopGroup();
        bootstrap
                .group(clientGroup)
                .channel(NioDatagramChannel.class)
                //.option(ChannelOption.SO_BROADCAST, true)
                .handler(new ChannelInitializer<DatagramChannel>() {
                    @Override
                    protected void initChannel(DatagramChannel ch) throws Exception {
                        ChannelPipeline pipeline=ch.pipeline();
                        pipeline.addLast(new SimpleChannelInboundHandler<DatagramPacket>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
                                System.out.println("[client] recv: "+msg.content().toString(CharsetUtil.UTF_8)+", from "+msg.sender());
                            }

                            @Override
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                DatagramPacket msg=new DatagramPacket(Unpooled.copiedBuffer("hello, this a echo from client!"
                                        .getBytes(CharsetUtil.UTF_8)),
                                        new InetSocketAddress("localhost",2233));
                                ctx.writeAndFlush(msg);
                            }
                        });
                    }
                });

        ChannelFuture future=bootstrap.bind(0);//这里端口号填0,实际上表示随机分配端口号
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if(future.isSuccess()) System.out.println("[client]: Channel bound!");
                else{
                    System.err.println("[client]: Bind attempt failed");
                    future.cause().printStackTrace();
                }
            }
        });
        DatagramPacket msg=new DatagramPacket(Unpooled.copiedBuffer("hello, this a echo from client!"
                .getBytes(CharsetUtil.UTF_8)),
                new InetSocketAddress("localhost",2233));
        future.channel().writeAndFlush(msg);
        try {
            future.sync();
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            clientGroup.shutdownGracefully().syncUninterruptibly();
        }
    }
}

UDP包的发送,完全是根据数据包里面的地址来的,上面的bind()绑定的只是本地端口

输出大概是这样的:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值