⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 routerserver.java

📁 使用Java语言编写模拟路由器程序
💻 JAVA
字号:
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;


public class RouterServer implements Runnable{
	private String rlfFile = System.getProperty("user.dir")+"\\rlf.txt";
	private String tcfFile = System.getProperty("user.dir")+"\\tcf.txt";
	
	public RouterServer(){
		int port = 1234;
		ServerSocket server = null;
		Socket socket = null;
		
		ObjectInputStream ois = null;
		ObjectOutputStream oos = null;
		
		FileWriter rlf = null;
		FileWriter tcf = null;
		
		ArrayList list = new ArrayList();
		
		try {
			File filerlf = new File(rlfFile);
			File filetcf = new File(tcfFile);
			if(filerlf.exists()== true){
				filerlf.delete();
			}
			if(filetcf.exists()== true){
				filetcf.delete();
			}
			
			rlf = new FileWriter(rlfFile,true);
			tcf = new FileWriter(tcfFile,true);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			server = new ServerSocket(port);
			System.out.println("RouterServer: Listening for routers on port "
					+ port);
		} catch (Exception e) {
			// TODO: handle exception
			System.out
					.println("RouterServer: Unable to open a listening socket on port: "
							+ port);
			System.out.println("Quitting...");
			System.exit(1);
		}
		
		while (true) {
			try {
				// accept a connection
				socket = server.accept();
				
			} catch (Exception e) {
//				e.printStackTrace();
				System.out.println("RouterServer: An I/O error occured while accepting a connection");
				continue;
			}

			try {
				// open the streams
				ois = new ObjectInputStream(socket.getInputStream());
				oos = new ObjectOutputStream(socket.getOutputStream());
				
				// read the command
				String rlfLine = "";
				String tcfLine = "";
				rlfLine = (String) ois.readObject();
				tcfLine = (String) ois.readObject();
				tcf.write(tcfLine);
				String strs[];
				String []rlfLines = rlfLine.split(":");
				if(rlfLines.length == 2){
					rlf.write(rlfLines[0]+"\n");
					strs = rlfLines[0].split(",");
					list.add(strs[strs.length-1]);
					
					if(rlfLines[1].equals("last"))
						break;
				}
				rlf.write(rlfLine+"\n");
				
				strs = rlfLine.split(",");
				list.add(strs[strs.length-1]);
				
				// close the streams and the connection
				ois.close();
				socket.close();
				
			} catch (Exception e) {
				System.out
						.println("RouterServer: An I/O error occured");
				continue;
			}
		}
		try {
			rlf.close();
			tcf.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		try {
			server.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		startRouter(rlfFile,tcfFile,list);
	}
	public void startRouter(String rlfFile, String tcfFile,ArrayList list){
		
		for (int i = 0; i < list.size(); i++) {
			Thread t = new Thread(this);
			t.setName((String)list.get(i));
			t.start();
		}
	}
	
	public void run(){
		Router r = new Router(rlfFile, tcfFile, Integer.parseInt(Thread.currentThread().getName()));
	}
	public static void main(String[] args) {
		RouterServer rs = new RouterServer();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -