当前位置: 首页>>代码示例>>Java>>正文


Java NioDatagramChannel类代码示例

本文整理汇总了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);
    }
}
 
开发者ID:Rsplwe,项目名称:Nukkit-Java9,代码行数:17,代码来源:UDPServerSocket.java

示例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();
}
 
开发者ID:talentchain,项目名称:talchain,代码行数:20,代码来源:UdpTest.java

示例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);
    }
}
 
开发者ID:JupiterDevelopmentTeam,项目名称:Jupiter,代码行数:17,代码来源:UDPServerSocket.java

示例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();
    }
}
 
开发者ID:jeikerxiao,项目名称:SpringBootStudy,代码行数:21,代码来源:UdpServer.java

示例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();
	}
}
 
开发者ID:rafradek,项目名称:Mods,代码行数:21,代码来源:TF2UdpServer.java

示例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));*/
}
 
开发者ID:rafradek,项目名称:Mods,代码行数:20,代码来源:TF2UdpClient.java

示例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();
	}
	
}
 
开发者ID:hdcuican,项目名称:java_learn,代码行数:22,代码来源:ChineseProverbClient.java

示例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();
    }
}
 
开发者ID:WarpOrganization,项目名称:warp,代码行数:22,代码来源:ConnectionService.java

示例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();
}
 
开发者ID:GoMint,项目名称:jRakNet,代码行数:40,代码来源:ClientSocket.java

示例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);
    }
}
 
开发者ID:BukkitPE,项目名称:BukkitPE,代码行数:19,代码来源:UDPServerSocket.java

示例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);
    }
}
 
开发者ID:PrismarineMC,项目名称:MagmaBlock,代码行数:17,代码来源:UDPServerSocket.java

示例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);
    }
}
 
开发者ID:voxelwind,项目名称:voxelwind,代码行数:22,代码来源:McpeOverRakNetNetworkListener.java

示例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);
}
 
开发者ID:SecureSmartHome,项目名称:SecureSmartHome,代码行数:19,代码来源:UDPDiscoveryServer.java

示例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);
}
 
开发者ID:duchien85,项目名称:netty-cookbook,代码行数:8,代码来源:BootstrapTemplate.java

示例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();
}
   }
 
开发者ID:changyuefeng,项目名称:netty-book,代码行数:21,代码来源:ChineseProverbClient.java


注:本文中的io.netty.channel.socket.nio.NioDatagramChannel类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。