使用java程式,監聽tcp協議埠
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
/**
*<h1>監聽tcp協議埠</h1>
* @Author Zyh
* @Date 2020/12/2
**/
@Slf4j
@Component
public class RadarTransferServer implements ApplicationRunner {
private Logger logger = LoggerFactory.getLogger(this.getClass());
//監聽的埠
private static final int port = 9031;
@Value("${third-interface-isprod}")
String isprod;
@Async
@Override
public void run(ApplicationArguments args) throws Exception {
if (isprod.equals("1")) {
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(group);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.option(ChannelOption.SO_REUSEADDR, true);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new MessageDecoder());
}
});
//緩衝區
//bootstrap.option(ChannelOption.SO_BACKLOG,1024);
//使用了Future來啟動執行緒,並繫結了埠
ChannelFuture future = bootstrap.bind(port).sync();
logger.info("啟動tcp伺服器啟動成功,正在監聽埠:" + port);
future.channel().closeFuture().await();//以非同步的方式關閉埠
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}}
還有一個類:
import com.cscec.smart.platform.service.DeviceWaterLevelService;
import com.cscec.smart.platform.util.SpringUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.string.StringDecoder;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import java.util.List;
@Slf4j
public class MessageDecoder extends StringDecoder {
//SpringUtil工具類,上個文章有,因為無法注入service、用工具獲取
@Resource
DeviceWaterLevelService deviceWaterLevelService = SpringUtil.getApplicationContext()
.getBean(DeviceWaterLevelService.class);
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
super.decode(ctx, msg, out);
int i = msg.readableBytes();
//我這個tcp協議推送的資料,都是固定值,我就直接擷取指定需要的欄位的就可以
byte[] deviceNoBytes = new byte[150];
ByteBuf byteBuf = msg.readBytes(deviceNoBytes);
String string = new String(deviceNoBytes);
String deviceNo = string.substring(0, 15);
String distance = string.substring(55, 60);
String waterLevel = string.substring(61, 66);
Boolean isWaterLevel = deviceWaterLevelService.saveDeviceWaterLevel(deviceNo, distance, waterLevel);
log.info("==========儲存機坪水位資料:{}",isWaterLevel);
}
}
相關文章
- Java Tcp協議socket程式設計學習JavaTCP協議程式設計
- 實用TCP協議(1):TCP 協議簡介TCP協議
- Java 網路程式設計 – TCP協議基本步驟Java程式設計TCP協議
- 系列TCP/IP協議-TCP協議概述(011)TCP協議
- tcp/ip協議TCP協議
- 使用WireShark抓包分析TCP協議TCP協議
- cifs協議埠協議
- 【網路協議】TCP協議簡介協議TCP
- DNS何時使用TCP與UDP協議?DNSTCPUDP協議
- Java中的TCP/IP協議和IP地址JavaTCP協議
- 初始化監聽埠
- 【Oracle】修改oracle監聽埠Oracle
- TCP協議詳解TCP協議
- TCP和UDP協議TCPUDP協議
- wireshark 分析TCP協議TCP協議
- TCP/IP協議族TCP協議
- TCP協議小結TCP協議
- 大話TCP協議TCP協議
- TCP/IP 協議族TCP協議
- TCP 協議簡介TCP協議
- 協議的埠號協議
- 埠集&協議集協議
- 使用wireshark分析TCP/IP協議中TCP包頭的格式TCP協議
- Linux(例如CentOS 7)開啟TCP 22埠,基於SSH協議LinuxCentOSTCP協議
- aix用命令查監聽埠對應的程式AI
- 網路通訊協議-TCP協議詳解!協議TCP
- Sqlserver重啟alwayson監聽埠SQLServer
- oracle 監聽配置多個埠Oracle
- 445埠被封之後,如何使用smb協議協議
- TCP應用層協議TCP協議
- TCP 協議有哪些缺陷?TCP協議
- TCP/IP五層協議TCP協議
- TCP/IP協議詳解TCP協議
- 網路協議之TCP協議TCP
- TCP IP之ICMP協議TCP協議
- TCP/IP網路協議TCP協議
- TCP協議資料格式TCP協議
- tcp /ip 協議簡介TCP協議