首页 > 编程知识 正文

物联网数据用什么数据库保存,物联网设备接入协议

时间:2023-05-04 09:24:56 阅读:11366 作者:3729

使用netty框架接收物联网数据1 .使用的pom.xml

dependencygroupidio.net ty/groupidartifactidnetty-all/artifact id/dependency2.客户端发送数据

import java.io.BufferedReader; import java.io.IOException; import Java.io.input streamreader; import java.io.PrintWriter; import java.net.Socket; import Java.net.unknownhostexception; printwith创建public class sockt { publicstaticvoidmain (字符串[ ] args ) try )//链接到服务器的8080端口的socket=newsocket(127 ) StringBuffer sb=new StringBuffer (上位机数据); bufferedreaderis=newbufferedreader (newinputstreamreader (socket.get inputstream () ); //发送数据pw.println(sb; pw.flush (; //接收数据String line=is.readLine (; System.out.println ('数据=='line ); //关闭资源pw.close (; is.close (; socket.close (; }catch(unknownhostexceptione )/todo auto-generatedcatchblocke.print stack trace ); }catch(ioexceptione )//todo auto-generatedcatchblocke.print stack trace ); }}3.服务器端使用netty接收数据

import io.net ty.bootstrap.server bootstrap; import io.netty.buffer.ByteBuf; import io.netty.channel.*; import io.net ty.channel.nio.nioeventloopgroup; import io.net ty.channel.socket.socket channel; import io.net ty.channel.socket.nio.nioserversocketchannel; import io.net ty.handler.codec.lengthfieldbasedframedecoder; import io.net ty.handler.codec.lengthfieldprepender; import io.net ty.handler.codec.messagetobyteencoder; importorg.spring framework.stereotype.com ponent; /** *服务器* * @ author */@ componentpublicclasstcpserver { public void run (} {//8080是要侦听的端口int port=8080; eventloopgroupboosgroup=newnioeventloopgroup (; eventloopgroupworkgroup=newnioeventloopgroup (; try { serverbootstrapbootstrap=new server bootstrap (; bootstrap.group(boosgroup,workGroup ).channel ) nioserversocketchannel.class ).childHandler ) newchannelinitializer

hannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new TCPHandler()); pipeline.addLast("encoder", new MessageToByteEncoder<byte[]>() { @Override protected void encode(ChannelHandlerContext ctx, byte[] msg, ByteBuf out) throws Exception { out.writeBytes(msg); } }); pipeline.addLast(new LengthFieldBasedFrameDecoder(65535 , 0, 2, 0, 2)); pipeline.addLast(new LengthFieldPrepender(2)); } }) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); System.out.println("服务启动..."); ChannelFuture channelFuture = bootstrap.bind(port).sync(); channelFuture.channel().closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } finally { workGroup.shutdownGracefully(); boosGroup.shutdownGracefully(); System.out.println("服务关闭..."); } }} import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.util.CharsetUtil;import org.springframework.stereotype.Component;import java.net.SocketAddress;import java.text.SimpleDateFormat;import java.util.Date;/** * @author */@Componentpublic class TCPHandler extends SimpleChannelInboundHandler<ByteBuf> { /** * 读取消息 * * @param ctx * @param m * @throws Exception */ @Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf m) throws Exception { byte[] data = new byte[m.readableBytes()]; m.readBytes(data); String msg = new String(data, CharsetUtil.UTF_8); SocketAddress address = ctx.channel().remoteAddress(); System.out.println(address.toString().substring(1) + "---" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); System.out.println(msg); } /** * 发生异常 * * @param ctx * @param cause * @throws Exception */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (ctx.channel().isActive()) { ctx.channel().writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } }} import com.example.socket.utils.TCPServer;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SocketApplication { public static void main(String[] args) { SpringApplication.run(SocketApplication.class, args); new Thread() { @Override public void run() { TCPServer server = new TCPServer(); server.run(); } }.start(); }}

yml文件

#springboot web访问端口server: port: 9055# netty配置netty: # 端口号 port: 6666 # 最大线程数 maxThreads: 1024 # 数据包的最大长度 max_frame_length: 65535

使用客户端发送数据后,服务器端就能接收到数据了

代码资源:https://download.csdn.net/download/kawayiyy123/57064752

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。