Compare commits
2 Commits
a9afea0603
...
497f55c40c
Author | SHA1 | Date | |
---|---|---|---|
497f55c40c | |||
638507e0f3 |
2
pom.xml
2
pom.xml
@ -18,7 +18,7 @@
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
<version>4.1.68.Final</version>
|
||||
<version>4.1.110.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
|
@ -38,6 +38,7 @@ public class RIPService {
|
||||
scheduler.scheduleAtFixedRate(() -> {
|
||||
RIPPacket packet = createRipResponsePacket(Config.RIP_VERSION);
|
||||
client.sendRipPacket(packet);
|
||||
System.out.println("客户端 发送了一次消息");
|
||||
}, 0, 10, TimeUnit.SECONDS);
|
||||
System.out.println("客户端已经启动");
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package cn.x47.config;
|
||||
|
||||
public class Config {
|
||||
// 设置协议版本,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 startClient = true;
|
||||
public static final boolean startClient = false;
|
||||
}
|
||||
|
@ -29,26 +29,22 @@ public class RIPPacketDecoder extends MessageToMessageDecoder<DatagramPacket> {
|
||||
while (buf.readableBytes() >= 20) {
|
||||
RIPEntry entry = new RIPEntry();
|
||||
entry.setAddressFamily(buf.readShort());
|
||||
if (ripPacket.getVersion() == 2) {
|
||||
entry.setRouteTag(buf.readShort());
|
||||
} else {
|
||||
entry.setRouteTag((short) 0);
|
||||
}
|
||||
entry.setRouteTag(buf.readShort());
|
||||
|
||||
byte[] ipBytes = new byte[4];
|
||||
buf.readBytes(ipBytes);
|
||||
entry.setIpAddress(InetAddress.getByAddress(ipBytes));
|
||||
|
||||
if (ripPacket.getVersion() == 2) {
|
||||
buf.readBytes(ipBytes);
|
||||
entry.setSubnetMask(InetAddress.getByAddress(ipBytes));
|
||||
buf.readBytes(ipBytes);
|
||||
entry.setNextHop(InetAddress.getByAddress(ipBytes));
|
||||
} else {
|
||||
buf.readBytes(ipBytes);
|
||||
entry.setSubnetMask(InetAddress.getByAddress(ipBytes));
|
||||
buf.readBytes(ipBytes);
|
||||
entry.setNextHop(InetAddress.getByAddress(ipBytes));
|
||||
if (ripPacket.getVersion() == 1) {
|
||||
// 对于 RIP v1,子网掩码和下一跳需要推断或设为默认值
|
||||
entry.setSubnetMask(InetAddress.getByName("255.255.255.0")); // 示例,实际应根据 IP 类推断
|
||||
entry.setNextHop(packet.sender().getAddress());
|
||||
}
|
||||
entry.setMetric(buf.readInt());
|
||||
entry.setMetric((int) buf.readUnsignedInt());
|
||||
entries.add(entry);
|
||||
}
|
||||
ripPacket.setEntries(entries);
|
||||
|
@ -11,6 +11,7 @@ public class RIPServerHandler extends SimpleChannelInboundHandler<RIPPacket> {
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, RIPPacket msg) throws Exception {
|
||||
System.out.println(msg);
|
||||
if (msg.getCommand() == 2) { // Response
|
||||
for (RIPEntry entry : msg.getEntries()) {
|
||||
// 更新本地路由表
|
||||
|
@ -11,9 +11,6 @@ import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.DatagramChannel;
|
||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
|
||||
public class RIPClient {
|
||||
|
||||
@ -36,8 +33,7 @@ public class RIPClient {
|
||||
|
||||
public void sendRipPacket(RIPPacket packet) {
|
||||
try {
|
||||
ChannelFuture future = bootstrap.bind(0)
|
||||
.sync();
|
||||
ChannelFuture future = bootstrap.bind(0).sync();
|
||||
future.channel().writeAndFlush(packet).sync();
|
||||
future.channel().close();
|
||||
} catch (InterruptedException e) {
|
||||
|
@ -4,7 +4,9 @@ package cn.x47.service;
|
||||
import cn.x47.handle.RIPPacketDecoder;
|
||||
import cn.x47.handle.RIPServerHandler;
|
||||
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.socket.DatagramChannel;
|
||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||
|
Loading…
x
Reference in New Issue
Block a user