优化 服务端,客户端代码 解决读取错位的问题。
This commit is contained in:
parent
638507e0f3
commit
497f55c40c
2
pom.xml
2
pom.xml
@ -18,7 +18,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.netty</groupId>
|
<groupId>io.netty</groupId>
|
||||||
<artifactId>netty-all</artifactId>
|
<artifactId>netty-all</artifactId>
|
||||||
<version>4.1.68.Final</version>
|
<version>4.1.110.Final</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
|
@ -2,7 +2,7 @@ package cn.x47.config;
|
|||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
// 设置协议版本,1 表示 RIP v1,2 表示 RIP v2
|
// 设置协议版本,1 表示 RIP v1,2 表示 RIP v2
|
||||||
public static final byte RIP_VERSION = 2;
|
public static final byte RIP_VERSION = 1;
|
||||||
public static final boolean startServer = true;
|
public static final boolean startServer = true;
|
||||||
public static final boolean startClient = false;
|
public static final boolean startClient = false;
|
||||||
}
|
}
|
||||||
|
@ -29,26 +29,22 @@ public class RIPPacketDecoder extends MessageToMessageDecoder<DatagramPacket> {
|
|||||||
while (buf.readableBytes() >= 20) {
|
while (buf.readableBytes() >= 20) {
|
||||||
RIPEntry entry = new RIPEntry();
|
RIPEntry entry = new RIPEntry();
|
||||||
entry.setAddressFamily(buf.readShort());
|
entry.setAddressFamily(buf.readShort());
|
||||||
if (ripPacket.getVersion() == 2) {
|
|
||||||
entry.setRouteTag(buf.readShort());
|
entry.setRouteTag(buf.readShort());
|
||||||
} else {
|
|
||||||
entry.setRouteTag((short) 0);
|
|
||||||
}
|
|
||||||
byte[] ipBytes = new byte[4];
|
byte[] ipBytes = new byte[4];
|
||||||
buf.readBytes(ipBytes);
|
buf.readBytes(ipBytes);
|
||||||
entry.setIpAddress(InetAddress.getByAddress(ipBytes));
|
entry.setIpAddress(InetAddress.getByAddress(ipBytes));
|
||||||
|
|
||||||
if (ripPacket.getVersion() == 2) {
|
|
||||||
buf.readBytes(ipBytes);
|
buf.readBytes(ipBytes);
|
||||||
entry.setSubnetMask(InetAddress.getByAddress(ipBytes));
|
entry.setSubnetMask(InetAddress.getByAddress(ipBytes));
|
||||||
buf.readBytes(ipBytes);
|
buf.readBytes(ipBytes);
|
||||||
entry.setNextHop(InetAddress.getByAddress(ipBytes));
|
entry.setNextHop(InetAddress.getByAddress(ipBytes));
|
||||||
} else {
|
if (ripPacket.getVersion() == 1) {
|
||||||
// 对于 RIP v1,子网掩码和下一跳需要推断或设为默认值
|
// 对于 RIP v1,子网掩码和下一跳需要推断或设为默认值
|
||||||
entry.setSubnetMask(InetAddress.getByName("255.255.255.0")); // 示例,实际应根据 IP 类推断
|
entry.setSubnetMask(InetAddress.getByName("255.255.255.0")); // 示例,实际应根据 IP 类推断
|
||||||
entry.setNextHop(packet.sender().getAddress());
|
entry.setNextHop(packet.sender().getAddress());
|
||||||
}
|
}
|
||||||
entry.setMetric(buf.readInt());
|
entry.setMetric((int) buf.readUnsignedInt());
|
||||||
entries.add(entry);
|
entries.add(entry);
|
||||||
}
|
}
|
||||||
ripPacket.setEntries(entries);
|
ripPacket.setEntries(entries);
|
||||||
|
@ -11,9 +11,6 @@ import io.netty.channel.nio.NioEventLoopGroup;
|
|||||||
import io.netty.channel.socket.DatagramChannel;
|
import io.netty.channel.socket.DatagramChannel;
|
||||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||||
|
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
|
|
||||||
|
|
||||||
public class RIPClient {
|
public class RIPClient {
|
||||||
|
|
||||||
@ -36,8 +33,7 @@ public class RIPClient {
|
|||||||
|
|
||||||
public void sendRipPacket(RIPPacket packet) {
|
public void sendRipPacket(RIPPacket packet) {
|
||||||
try {
|
try {
|
||||||
ChannelFuture future = bootstrap.bind(0)
|
ChannelFuture future = bootstrap.bind(0).sync();
|
||||||
.sync();
|
|
||||||
future.channel().writeAndFlush(packet).sync();
|
future.channel().writeAndFlush(packet).sync();
|
||||||
future.channel().close();
|
future.channel().close();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
@ -4,7 +4,9 @@ package cn.x47.service;
|
|||||||
import cn.x47.handle.RIPPacketDecoder;
|
import cn.x47.handle.RIPPacketDecoder;
|
||||||
import cn.x47.handle.RIPServerHandler;
|
import cn.x47.handle.RIPServerHandler;
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.channel.*;
|
import io.netty.channel.ChannelFuture;
|
||||||
|
import io.netty.channel.ChannelInitializer;
|
||||||
|
import io.netty.channel.ChannelOption;
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
import io.netty.channel.socket.DatagramChannel;
|
import io.netty.channel.socket.DatagramChannel;
|
||||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user