本文整理汇总了Java中io.netty.channel.socket.nio.NioDatagramChannel类的典型用法代码示例。如果您正苦于以下问题:Java NioDatagramChannel类的具体用法?Java NioDatagramChannel怎么用?Java NioDatagramChannel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NioDatagramChannel类属于io.netty.channel.socket.nio包,在下文中一共展示了NioDatagramChannel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: UDPServerSocket
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public UDPServerSocket(ThreadedLogger logger, int port, String interfaz) {
this.logger = logger;
try {
bootstrap = new Bootstrap();
group = new NioEventLoopGroup();
bootstrap
.group(group)
.channel(NioDatagramChannel.class)
.handler(this);
channel = bootstrap.bind(interfaz, port).sync().channel();
} catch (Exception e) {
this.logger.critical("**** FAILED TO BIND TO " + interfaz + ":" + port + "!");
this.logger.critical("Perhaps a server is already running on that port?");
System.exit(1);
}
}
示例2: create
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public Channel create(String bindAddr, int port) throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup(1);
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
public void initChannel(NioDatagramChannel ch)
throws Exception {
ch.pipeline().addLast(new PacketDecoder());
SimpleMessageHandler messageHandler = new SimpleMessageHandler(ch, nodeManager);
nodeManager.setMessageSender(messageHandler);
ch.pipeline().addLast(messageHandler);
}
});
return b.bind(bindAddr, port).sync().channel();
}
示例3: UDPServerSocket
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public UDPServerSocket(ThreadedLogger logger, int port, String interfaz) {
this.logger = logger;
try {
bootstrap = new Bootstrap();
group = new NioEventLoopGroup();
bootstrap
.group(group)
.channel(NioDatagramChannel.class)
.handler(this);
channel = bootstrap.bind(interfaz, port).sync().channel();
} catch (Exception e) {
this.logger.critical(FastAppender.get(interfaz, ":", port, " 上でサーバーを開けませんでした。"));
this.logger.critical("同じポートで複数のサーバーを一度に開いていませんか?");
System.exit(1);
}
}
示例4: run
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
@Async("myTaskAsyncPool")
public void run(int udpReceivePort) {
EventLoopGroup group = new NioEventLoopGroup();
logger.info("Server start! Udp Receive msg Port:" + udpReceivePort);
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new UdpServerHandler()); // 设置服务端接收消息的 Handler (保存消息到 mysql 和 redis 中)
b.bind(udpReceivePort).sync().channel().closeFuture().await();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
示例5: run
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public void run() {
try {
Bootstrap boot = new Bootstrap();
boot.group(group)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
protected void initChannel(DatagramChannel ch) throws Exception {
channel = ch;
ch.pipeline().addLast(new UdpChannelHandlerServer(TF2UdpServer.this));
}
});
boot.bind(port).sync().channel().closeFuture();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
示例6: TF2UdpClient
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public TF2UdpClient(InetSocketAddress address) throws Exception {
group = new NioEventLoopGroup();
this.address = address;
EntityPlayerSP player = Minecraft.getMinecraft().player;
Bootstrap boot = new Bootstrap();
boot.group(group).channel(NioDatagramChannel.class).handler(new UdpChannelHandlerClient());
channel = boot.bind(0).sync().channel();
channel.connect(address);
/*PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
buffer.writeShort(playerId);
buffer.writeShort(0);
buffer.writeByte(0);
buffer.writeLong(System.currentTimeMillis());
channel.writeAndFlush(new DatagramPacket(buffer, address));*/
}
示例7: main
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
EventLoopGroup loopGroup = new NioEventLoopGroup();
try{
Bootstrap b = new Bootstrap();
b.group(loopGroup).channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new ChannelProverbClientHandler());
Channel ch = b.bind(0).sync().channel();
//向网段内所有的机器广播UDP消息
ch.writeAndFlush(
new DatagramPacket(Unpooled.copiedBuffer("谚语字典查询?", CharsetUtil.UTF_8),
new InetSocketAddress("255.255.255.255", port))).sync();
//客户端等待15s接受服务端的应答消息,然后退出释放资源
if(!ch.closeFuture().await(15000)){
System.out.println("查询超时");
}
}finally{
loopGroup.shutdownGracefully();
}
}
示例8: ConnectionService
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public ConnectionService(SerializedSceneHolder sceneHolder,
ClientRemoteEventQueue eventQueue,
ComponentRegistry componentRegistry) {
try {
Bootstrap b = new Bootstrap();
ServerConnectionHandler connectionHandler = new ServerConnectionHandler(
sceneHolder,
this,
eventQueue,
new EventReceiver(componentRegistry));
b.group(group)
.channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(connectionHandler);
channel = b.bind(0).sync().channel();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
示例9: initialize
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
/**
* Initializes this socket and binds its internal udp socket to a free port.
* If the socket is already initialized any invocation of this method will
* result in an IllegalStateException.
*
* @throws SocketException Thrown in case the socket could not be initialized
*/
public void initialize() throws SocketException {
if ( this.isInitialized() ) {
throw new IllegalStateException( "Cannot re-initialized ClientSocket" );
}
this.udpSocket = new Bootstrap();
this.udpSocket.group( Epoll.isAvailable() ? new EpollEventLoopGroup() : new NioEventLoopGroup() );
this.udpSocket.channel( Epoll.isAvailable() ? EpollDatagramChannel.class : NioDatagramChannel.class );
this.udpSocket.handler( new ChannelInboundHandlerAdapter() {
@Override
public void channelRead( ChannelHandlerContext ctx, Object msg ) throws Exception {
io.netty.channel.socket.DatagramPacket packet = (io.netty.channel.socket.DatagramPacket) msg;
PacketBuffer content = new PacketBuffer( packet.content() );
InetSocketAddress sender = packet.sender();
if ( !receiveDatagram( sender, content ) ) {
// Push datagram to update queue:
handleDatagram( sender, content, System.currentTimeMillis() );
}
}
} );
try {
this.channel = this.udpSocket.bind( ThreadLocalRandom.current().nextInt( 45000, 65000 ) ).sync().channel();
} catch ( InterruptedException e ) {
SocketException exception = new SocketException( "Could not bind to socket" );
exception.initCause( e );
throw exception;
}
this.afterInitialize();
}
示例10: UDPServerSocket
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public UDPServerSocket(ThreadedLogger logger, int port, String interfaz) {
this.logger = logger;
try {
bootstrap = new Bootstrap();
group = new NioEventLoopGroup();
bootstrap
.group(group)
.channel(NioDatagramChannel.class)
.handler(this);
channel = bootstrap.bind(interfaz, port).sync().channel();
} catch (InterruptedException e) {
this.logger.critical("**** FAILED TO BIND TO " + interfaz + ":" + port + "!");
this.logger.critical("-------------------------------------------------");
this.logger.critical("There may be another server running on that port!");
this.logger.critical("--------------------------------------------------");
System.exit(1);
}
}
示例11: UDPServerSocket
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public UDPServerSocket(ThreadedLogger logger, int port, String interfaz) {
this.logger = logger;
try {
bootstrap = new Bootstrap();
group = new NioEventLoopGroup();
bootstrap
.group(group)
.channel(NioDatagramChannel.class)
.handler(this);
channel = bootstrap.bind(interfaz, port).sync().channel();
} catch (InterruptedException e) {
this.logger.critical("**** FAILED TO BIND TO " + interfaz + ":" + port + "!");
this.logger.critical("Perhaps a server is already running on that port?");
System.exit(1);
}
}
示例12: McpeOverRakNetNetworkListener
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public McpeOverRakNetNetworkListener(VoxelwindServer voxelwindServer, String host, int port, boolean useSoReuseport) {
this.server = voxelwindServer;
this.address = new InetSocketAddress(host, port);
this.useSoReuseport = useSoReuseport;
if (Epoll.isAvailable()) {
bootstrap = new Bootstrap()
.channel(EpollDatagramChannel.class)
.group(new EpollEventLoopGroup(0, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Voxelwind MCPE Listener - #%d").build()))
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.handler(this);
if (useSoReuseport) {
bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
}
} else {
bootstrap = new Bootstrap()
.channel(NioDatagramChannel.class)
.group(new NioEventLoopGroup(0, new ThreadFactoryBuilder().setDaemon(true).setNameFormat("Voxelwind MCPE Listener - #%d").build()))
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
.handler(this);
}
}
示例13: init
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
@Override
public void init(Container container) {
super.init(container);
// Acquire lock
final WifiManager wifi = (WifiManager) requireComponent(ContainerService.KEY_CONTEXT)
.getSystemService(Context.WIFI_SERVICE);
multicastLock = wifi.createMulticastLock(getClass().getSimpleName());
multicastLock.acquire();
// Setup UDP Channel
Bootstrap b = new Bootstrap()
.channel(NioDatagramChannel.class)
.group(requireComponent(ExecutionServiceComponent.KEY))
.handler(new RequestHandler())
.option(ChannelOption.SO_BROADCAST, true);
channel = b.bind(CoreConstants.NettyConstants.DISCOVERY_SERVER_PORT);
}
示例14: newBootstrapUDP
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public static ChannelFuture newBootstrapUDP(EventLoopGroup loopGroup, SimpleChannelInboundHandler<DatagramPacket> handler, int port){
return new Bootstrap().group(loopGroup)
.channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(handler)
.bind(port);
}
示例15: run
import io.netty.channel.socket.nio.NioDatagramChannel; //导入依赖的package包/类
public void run(int port) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true)
.handler(new ChineseProverbClientHandler());
Channel ch = b.bind(0).sync().channel();
// 向网段内的所有机器广播UDP消息
ch.writeAndFlush(
new DatagramPacket(Unpooled.copiedBuffer("谚语字典查询?",
CharsetUtil.UTF_8), new InetSocketAddress(
"255.255.255.255", port))).sync();
if (!ch.closeFuture().await(15000)) {
System.out.println("查询超时!");
}
} finally {
group.shutdownGracefully();
}
}