优化 服务端 客户端代码

This commit is contained in:
2024-12-01 16:50:43 +08:00
parent 6203c43ebe
commit a9afea0603
2 changed files with 25 additions and 20 deletions

View File

@ -18,26 +18,29 @@ public class RIPService {
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
// 启动服务器 // 启动服务器
// RIPServer server = new RIPServer(); if (Config.startServer) {
// new Thread(() -> { RIPServer server = new RIPServer();
// try { new Thread(() -> {
// server.start(); try {
// } catch (InterruptedException e) { server.start();
// e.printStackTrace(); } catch (InterruptedException e) {
// } e.printStackTrace();
// }).start(); }
}).start();
System.out.println("服务端已经启动");
}
// 启动客户端 if (Config.startClient) {
RIPClient client = new RIPClient(); // 启动客户端
RIPClient client = new RIPClient();
// 定期发送路由更新 // 定期发送路由更新
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(() -> { scheduler.scheduleAtFixedRate(() -> {
System.out.println("发送 开始"); RIPPacket packet = createRipResponsePacket(Config.RIP_VERSION);
RIPPacket packet = createRipResponsePacket(Config.RIP_VERSION); client.sendRipPacket(packet);
client.sendRipPacket(packet); }, 0, 10, TimeUnit.SECONDS);
System.out.println("发送 结束"); System.out.println("客户端已经启动");
}, 0, 10, TimeUnit.SECONDS); }
// 主线程等待 // 主线程等待
Thread.currentThread().join(); Thread.currentThread().join();

View File

@ -2,5 +2,7 @@ package cn.x47.config;
public class Config { public class Config {
// 设置协议版本1 表示 RIP v12 表示 RIP v2 // 设置协议版本1 表示 RIP v12 表示 RIP v2
public static final byte RIP_VERSION = 1; public static final byte RIP_VERSION = 2;
public static final boolean startServer = true;
public static final boolean startClient = true;
} }