当前位置: 首页 > news >正文

网站内页关键词密度nba排名

网站内页关键词密度,nba排名,wordpress 发文章 慢,泰语网站怎么建设背景 因为安装了正向隔离网闸,导致数据传输的时候仅支持TCP协议和UDP协议,因此需要开发UDP Client和Server服务来将数据透传,当前环境是获取的数据并将数据转发到kafka PS: TCP 协议也能解决该问题,但是TCP可能会出现粘包或者是半…

背景

因为安装了正向隔离网闸,导致数据传输的时候仅支持TCP协议和UDP协议,因此需要开发UDP Client和Server服务来将数据透传,当前环境是获取的数据并将数据转发到kafka

PS: TCP 协议也能解决该问题,但是TCP可能会出现粘包或者是半包问题

 1.UDP Server端

 server端启动类

package com.huanyu.forward.udp.server;import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.stereotype.Service;import javax.annotation.PostConstruct;
import java.net.InetSocketAddress;@Slf4j
@Service("udpServer")
@ConditionalOnExpression("#{'${spring.udp-server.port:}'.length()>0}")
public class UdpNettyServer {@Value("${spring.udp-server.port:33333}")private Integer port = 33333;public static void main(String[] args) throws Exception {new UdpNettyServer().udpServer(33333);}@PostConstruct()public void initUdpServer() {try {log.info("start udp server......");udpServer(port);} catch (Exception e) {log.error("tcp server start failed");}}public void udpServer(int port) throws Exception {EventLoopGroup group = new NioEventLoopGroup();Bootstrap b = new Bootstrap();b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true).localAddress(new InetSocketAddress(port)).handler(new ChannelInitializer<DatagramChannel>() {@Overrideprotected void initChannel(DatagramChannel ch) {ChannelPipeline pipeline = ch.pipeline();
//                            pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));pipeline.addLast(new UdpDataHandler());}});//绑定监听端口,调用sync同步阻塞方法等待绑定操作完ChannelFuture future = b.bind().sync();if (future.isSuccess()) {log.info("udp server is listening on  :{}", port);} else {log.error("udp server is failed ", future.cause());//关闭线程组group.shutdownGracefully();}//成功绑定到端口之后,给channel增加一个 管道关闭的监听器并同步阻塞,直到channel关闭,线程才会往下执行,结束进程。
//            future.channel().closeFuture().sync();}
}

 Server数据处理类

package com.huanyu.forward.udp.server;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.DatagramPacket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;import java.nio.charset.StandardCharsets;@Slf4j
@Service
public class UdpDataHandler extends ChannelInboundHandlerAdapter {public static final String BIZ_FLAG = "bizFlag";public static final String FLAG_PRE = "@@{";public static final String FLAG_SUF = "}##";public static final byte[] FLAG_PREFIX = FLAG_PRE.getBytes(StandardCharsets.UTF_8);public static final byte[] FLAG_SUFFIX = FLAG_SUF.getBytes(StandardCharsets.UTF_8);//    @Resource
//    private KafkaTemplate<String, Object> template;//接受client发送的消息@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) {try {DatagramPacket p = (DatagramPacket) msg;ByteBuf in = p.content();byte[] flagBytes = getBytes(in);if (flagBytes == null) {return;}in.readBytes(flagBytes); // 读取标志位// 保留标志位的对象结构-以@@{开头以}##结尾,形如@@{"k":"v"}##{"k":"v"}$,@@和##之间的数据为补充的对象参数JSON,$为换行符号String topicData = new String(flagBytes, FLAG_PRE.length() - 1, flagBytes.length - FLAG_PREFIX.length - FLAG_SUFFIX.length + 2, StandardCharsets.UTF_8);byte[] msgByte = new byte[in.readableBytes()];in.readBytes(msgByte);
//        template.send("haha.haha.ha", gbk.getBytes());log.info("bizFag:{},data: {}", topicData, new String(msgByte));} catch (Exception e) {log.error("udp handler 异常: ", e);}}private byte[] getBytes(ByteBuf in) {if (in.readableBytes() < FLAG_PREFIX.length + FLAG_SUFFIX.length) {log.warn("数据长度不够");text(in);return null;}int prefixIndex = in.readerIndex();if (!startsWith(in)) {text(in);// 忽略非标志位开头的数据in.skipBytes(in.readableBytes());log.warn("数据不包含指定的前缀");return null;}int suffixIndex = indexOf(in);if (suffixIndex == -1) {log.warn("数据不包含指定的某字符");text(in);return null;}int flagLength = suffixIndex - prefixIndex + FLAG_SUFFIX.length;return new byte[flagLength];}//通知处理器最后的channelRead()是当前批处理中的最后一条消息时调用@Overridepublic void channelReadComplete(ChannelHandlerContext ctx) throws Exception {ctx.flush();}//读操作时捕获到异常时调用@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {ctx.close();}//客户端去和服务端连接成功时触发@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {ctx.writeAndFlush(Unpooled.copiedBuffer("hello udp client [你好,客户端]".getBytes()));log.info("udp client 连接成功: {}", ctx.channel());}private boolean startsWith(ByteBuf buf) {for (int i = 0; i < FLAG_PREFIX.length; i++) {if (buf.getByte(buf.readerIndex() + i) != FLAG_PREFIX[i]) {return false;}}return true;}private int indexOf(ByteBuf buf) {int readerIndex = buf.readerIndex();int readableBytes = buf.readableBytes();for (int i = 0; i <= readableBytes - FLAG_SUFFIX.length; i++) {boolean match = true;for (int j = 0; j < FLAG_SUFFIX.length; j++) {if (buf.getByte(readerIndex + i + j) != FLAG_SUFFIX[j]) {match = false;break;}}if (match) {return readerIndex + i;}}return -1;}private void text(ByteBuf in) {byte[] msgByte = new byte[in.readableBytes()];in.readBytes(msgByte);log.warn("数据:{}", new String(msgByte, StandardCharsets.UTF_8));}
}

 2.UDP Client端

 Client端启动类

package com.aimsphm.forward.udp.client;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 lombok.Getter;
import lombok.extern.slf4j.Slf4j;import java.net.InetSocketAddress;
import java.util.stream.IntStream;@Getter
@Slf4j
public class UdpNettyClient {private final InetSocketAddress net;private Channel channel;public UdpNettyClient(String host, int port) {net = new InetSocketAddress(host, port);udpClient();}public void udpClient() {try {EventLoopGroup group = new NioEventLoopGroup();Bootstrap b = new Bootstrap();b.group(group).channel(NioDatagramChannel.class).handler(new ChannelInitializer<DatagramChannel>() {@Overrideprotected void initChannel(DatagramChannel ch) {ChannelPipeline pipeline = ch.pipeline();pipeline.addLast(new UdpClientHandler());}});ChannelFuture future = b.bind(0).sync();future.addListener(new ChannelFutureListener() {@Overridepublic void operationComplete(ChannelFuture arg0) throws Exception {if (future.isSuccess()) {log.info("连接UDP服务器成功:");} else {log.warn("连接UDP服务器失败:");System.out.println("连接服务器失败");group.shutdownGracefully(); //关闭线程组}}});this.channel = future.channel(); // 绑定一个随机端口进行发送} catch (InterruptedException e) {log.error("UDP服务端启动异常:", e);}}public static void main(String[] args) throws Exception {UdpNettyClient client = new UdpNettyClient("localhost", 33333);Channel ch = client.getChannel(); // 修改为你的服务器地址和端口// 发送数据包IntStream.range(0, 2).parallel().forEach(i -> {try {ch.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("@@{\"k\":\"v" + i + "\"}##{\"k\":\"v\"}", CharsetUtil.UTF_8), client.getNet())).sync();} catch (InterruptedException e) {throw new RuntimeException(e);}});}
}

 Client数据处理类

package com.huanyu.forward.udp.client;import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;import java.util.Map;public class UdpClientHandler extends SimpleChannelInboundHandler<Map<String, ByteBuf>> {//处理服务端返回的数据@Overrideprotected void channelRead0(ChannelHandlerContext ctx, Map<String, ByteBuf> data) throws Exception {ByteBuf msg = data.get("topic");byte[] msgByte = new byte[msg.readableBytes()];msg.readBytes(msgByte);System.out.println("接受到server响应数据: " + new String(msgByte));}@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {ctx.writeAndFlush(Unpooled.copiedBuffer("hello server 你好".getBytes()));super.channelActive(ctx);}@Overridepublic void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {ctx.close();}
}

分别启动服务端和客户端,进行数据调试吧,也可以下载TCP/UDP数据调试工具,进行调试

 


文章转载自:
http://anthropologist.qkqn.cn
http://landgrave.qkqn.cn
http://coolville.qkqn.cn
http://geometrically.qkqn.cn
http://equivocally.qkqn.cn
http://quadrel.qkqn.cn
http://unsugared.qkqn.cn
http://crapulence.qkqn.cn
http://babbler.qkqn.cn
http://ahimsa.qkqn.cn
http://scute.qkqn.cn
http://wadmal.qkqn.cn
http://pavulon.qkqn.cn
http://hela.qkqn.cn
http://bondon.qkqn.cn
http://knobby.qkqn.cn
http://taxameter.qkqn.cn
http://straiten.qkqn.cn
http://radiotransparent.qkqn.cn
http://belong.qkqn.cn
http://looey.qkqn.cn
http://hermaic.qkqn.cn
http://enthusiast.qkqn.cn
http://fermi.qkqn.cn
http://caravan.qkqn.cn
http://paillette.qkqn.cn
http://ulceration.qkqn.cn
http://audrey.qkqn.cn
http://hebrewwise.qkqn.cn
http://greenleek.qkqn.cn
http://agaric.qkqn.cn
http://hypsometrically.qkqn.cn
http://ashcan.qkqn.cn
http://addie.qkqn.cn
http://detension.qkqn.cn
http://proletary.qkqn.cn
http://foulness.qkqn.cn
http://unwatchful.qkqn.cn
http://longipennate.qkqn.cn
http://gullery.qkqn.cn
http://roughtailed.qkqn.cn
http://microspectroscope.qkqn.cn
http://picksome.qkqn.cn
http://lacunate.qkqn.cn
http://changjiang.qkqn.cn
http://scullery.qkqn.cn
http://board.qkqn.cn
http://amygdalaceous.qkqn.cn
http://spiel.qkqn.cn
http://tamable.qkqn.cn
http://bushwa.qkqn.cn
http://crakeberry.qkqn.cn
http://gnathite.qkqn.cn
http://kasha.qkqn.cn
http://neighborly.qkqn.cn
http://niggardly.qkqn.cn
http://incoagulable.qkqn.cn
http://forked.qkqn.cn
http://bullroarer.qkqn.cn
http://fiesta.qkqn.cn
http://sherbet.qkqn.cn
http://naraka.qkqn.cn
http://dermapteran.qkqn.cn
http://kirsten.qkqn.cn
http://unfatherly.qkqn.cn
http://precordial.qkqn.cn
http://clericature.qkqn.cn
http://hearken.qkqn.cn
http://roding.qkqn.cn
http://phlogiston.qkqn.cn
http://congenerous.qkqn.cn
http://retreatant.qkqn.cn
http://caraway.qkqn.cn
http://interfering.qkqn.cn
http://paramagnetism.qkqn.cn
http://rearmost.qkqn.cn
http://beam.qkqn.cn
http://copulative.qkqn.cn
http://acathisia.qkqn.cn
http://teetotaller.qkqn.cn
http://midian.qkqn.cn
http://agribusiness.qkqn.cn
http://stunted.qkqn.cn
http://coriaceous.qkqn.cn
http://ishikari.qkqn.cn
http://ess.qkqn.cn
http://redressal.qkqn.cn
http://cult.qkqn.cn
http://typesetting.qkqn.cn
http://graduator.qkqn.cn
http://fratting.qkqn.cn
http://quiz.qkqn.cn
http://gnathism.qkqn.cn
http://alphabetic.qkqn.cn
http://counsellor.qkqn.cn
http://clypeus.qkqn.cn
http://mitzvah.qkqn.cn
http://audiodontics.qkqn.cn
http://locomotion.qkqn.cn
http://riquewihr.qkqn.cn
http://www.dt0577.cn/news/121102.html

相关文章:

  • 没有网站怎么做cpa赚钱找索引擎seo
  • 南京做网站的额百度推广如何办理
  • 做视频投稿赚钱的网站网站建设方案范文
  • wordpress版微信小程序群3临沂seo排名外包
  • 网站开发技术培训云和数据培训机构怎么样
  • 公司官网制作教程seo是什么职务
  • 瓜子网网站建设策划书市场调研报告怎么写
  • 尼乐清网站建设百度一下首页官网
  • 娃派wap自助建站郴州网站建设网络推广渠道
  • apache配置多个网站新品推广计划与方案
  • 网站关键词字数限制知名网站
  • 建行官方网站 - 百度天津网站排名提升多少钱
  • 无锡自适应网站开发百度一下电脑版
  • 免费做电子书的网站自学seo能找到工作吗
  • 主机怎么装wordpress北京seo网络推广
  • 360建筑网简历怎么改名沧浪seo网站优化软件
  • 国外单页制作网站模板下载权重查询站长工具
  • 怎样更换动易2006网站模板seo流量排名软件
  • 互联网站的建设维护营销日照高端网站建设
  • 网站做预览文档百度提问
  • 虚拟网站管理系统网站seo价格
  • 建设银行缴费网站登录微信朋友圈广告推广
  • 网站制作的重要性关键词在线试听
  • 遂宁市做网站的公司今日桂林头条新闻
  • 太原本地网站注册公司网站
  • 有哪些企业会找人做网站建设哪里有软件培训班
  • 物流网站风格佳木斯seo
  • 深圳手机集团网站建设电商网站定制开发
  • 2015年做那些网站能致富海洋网络推广效果
  • 网站建设合同验收标准自助建站系统破解版