UDP是一种无连接协议,相对于TCP协议而言,简单高效,适用于传输视频、音频等及时性要求高,但是准确率要求低的数据。Netty对UDP传输数据也进行了封装,实现起来特别简单。
首先是编写服务端启动类:
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioDatagramChannel;
public class ChineseProverbServer
{
public void run(int port)throws Exception{
EventLoopGroup bossGroup=new NioEventLoopGroup();
try
{
//通过NioDatagramChannel创建Channel,并设置Socket参数支持广播
//UDP相对于TCP不需要在客户端和服务端建立实际的连接,因此不需要为连接(ChannelPipeline)设置handler
Bootstrap b=new Bootstrap();
b.group(bossGroup)
.channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new ChineseProverbServerHandler());
b.bind(port).sync().channel().closeFuture().await();
}
catch (Exception e)
{
e.printStackTrace();
}
finally{
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args)throws Exception
{
in
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务