NETTY实战
NETTY实战
东门非树 发表于3年前
NETTY实战
  • 发表于 3年前
  • 阅读 336
  • 收藏 0
  • 点赞 0
  • 评论 4

【腾讯云】如何购买服务器最划算?>>>   

摘要: 由于项目通信使用到netty,所以记录下。

//TCP监听

public void run(){
  try {
   this.logger.info("监听TCP端口:" + String.valueOf(this.port));
   b.group(bossGroup, workerGroup)
   .channel(NioServerSocketChannel.class)
   .option(ChannelOption.SO_BACKLOG, 1000)
   .handler(new LoggingHandler(LogLevel.INFO))
   .childHandler(new ChannelInitializer<SocketChannel>(){
    @Override
    protected void initChannel(SocketChannel ch)
      throws Exception {
     ch.pipeline().addLast(new TCPChannelHandlerAdapter(port, logger));
    }
   });   
   b.bind(port).sync().channel().closeFuture().sync();   
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  }
  finally{
   bossGroup.shutdownGracefully();
   workerGroup.shutdownGracefully();
  }

  }

 

 

//UDP监听

public void run()
 {
  //EventLoopGroup group = new NioEventLoopGroup();
        try {
            this.logger.info("监听UDP端口:" + String.valueOf(this.port));
            b.group(bossGroup)
             .channel(NioDatagramChannel.class)
             .option(ChannelOption.SO_BACKLOG, 1000)
             .option(ChannelOption.SO_BROADCAST, true)
             .handler(new UDPChannelHandlerAdapter(port,this.server, this.udpchannels));
            //System.out.println("UDP:"+port+" Server is starting……"); 
            b.bind(port).sync().channel().closeFuture().await();
          
        }
        catch (Exception e) {
         e.printStackTrace();
  }
        finally {
            bossGroup.shutdownGracefully();
        }

 }

共有 人打赏支持
粉丝 0
博文 10
码字总数 4040
评论 (4)
风声云起
博主,udp发送消息呢?
东门非树

引用来自“风声云起”的评论

博主,udp发送消息呢?
public static void main(String[] args) throws Exception { UdpClientSocket client = new UdpClientSocket(); String serverHost = "58.213.150.66"; //58.213.150.66 //String serverHost = "10.1.2.133"; int serverPort = 25002; for(int i=2;i>1;i++){ System.out.println("BDJY,863121020017075,20171118143757,118.698815,32.161784,Net+GPS,69,1,,\n" + ("BDJY,863121020017075,20171118143757,118.698815,32.161784,Net+GPS,69,1,,\\n").getBytes().toString()); client.send(serverHost, serverPort, ("BDJY,863121020017075,20171118143757,118.698815,32.161784,Net+GPS,69,1,,\\n").getBytes()); System.out.println("发送数据:" + client.getSocket()); } //String info = client.receive(serverHost, serverPort); //System.out.println("服务端回应数据:" + info); } }
东门非树

引用来自“风声云起”的评论

博主,udp发送消息呢?
public final DatagramPacket send(final String host, final int port, final byte[] bytes) throws IOException { DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress .getByName(host), port); ds.send(dp); return dp; }
东门非树

引用来自“风声云起”的评论

博主,udp发送消息呢?
public class UdpClientSocket { private byte[] buffer = new byte[1024]; private DatagramSocket ds = null; public UdpClientSocket() throws Exception { ds = new DatagramSocket(); } public final void setSoTimeout(final int timeout) throws Exception { ds.setSoTimeout(timeout); } public final int getSoTimeout() throws Exception { return ds.getSoTimeout(); } public final DatagramSocket getSocket() { return ds; } public final String receive(final String lhost, final int lport) throws Exception { DatagramPacket dp = new DatagramPacket(buffer, buffer.length); ds.receive(dp); String info = new String(dp.getData(), 0, dp.getLength()); return info; } public final void close() { try { ds.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
×
东门非树
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: