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

📄 bfnode.java

📁 使用Java语言编写模拟路由器程序
💻 JAVA
字号:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;



public class bfnode {
	static{
		/*String lockFile = ".lock";
		File file = new File(lockFile);
		try {
			RandomAccessFile fis = new RandomAccessFile("RouterServer.class.lock",
					"rw");
			FileChannel lockfc = fis.getChannel();
			FileLock flock = lockfc.tryLock();
			if (flock == null) {
				System.out.println("running...");
			} else {
				System.out.println("init...");
				Process p = null;
				try {
					p = Runtime.getRuntime().exec("java RouterServer");
					//Thread.sleep(10000);
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					//p.destroy();
				}
			}
		} catch (Exception e) {

		}*/
		
	}
	public static void prompt(){
		System.out.println("Usage: bfnode <local-port> <neighbor1-port> <distance1> <neighbor2-port> <distance2> ... <last>");
		System.out.println("<local-port>:		The UDP listening port number of the node");
		System.out.println("<neighbor-port>:	The UDP listening port number of one of the neighboring nodes");
		System.out.println("<distance>:		The link distance to the <neighbor1-port>");
		System.out.println("			Keep listing the pair of <neighbor-port> and <distance> for all your neighboring nodes");
		System.out.println("<last>:			Indication of the last node information of the network. Upon the input of the command");
		System.out.println("			with this argument, the routing message exchanges among the nodes should kick in");
		System.out.println("exit(ctrl-C): 		Exit the program");
		System.exit(1);
	}
	//parse command info
	public static void main(String[] args) {
		if(args.length < 3 || !((args.length-1)%2 == 0 || (args.length-1)%2 == 1)){
			prompt();
		}
		String ip = "";
		int local_port;
		int neighbor_port[] = new int[(args.length-1)/2];
		int distance[] = new int[(args.length-1)/2];
		String last = null;
		
		String rlfContent = "";
		String tcfContent = "";
		
//		192.168.199.228,10001,11001,1
		InetAddress host = null;
		try {
			host = InetAddress.getByName("192.168.199.228");
			ip = host.getHostAddress();
		} catch (UnknownHostException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		local_port = Integer.parseInt(args[0]);
		
		rlfContent = ip+","+args[0]+","+new Integer((local_port+1000)).toString()+"," + args[0];
		/*try {
			//fill rlf.txt
			rlf.write((host.getHostAddress()+",").getBytes());
			rlf.write((args[0]+",").getBytes());
			rlf.write((new Integer((local_port+1000)).toString()+",").getBytes());
			rlf.write((args[0]+"\n").getBytes());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}*/
		
		for (int i = 0; i < neighbor_port.length; i++) {
			neighbor_port[i] = Integer.parseInt(args[2 * i + 1]);
			distance[i] = Integer.parseInt(args[2 * i + 2]);
	
			tcfContent += args[0] + "," + args[2 * i + 1] + ","
					+ args[2 * i + 2] + "\n";
			// fill tcf.txt
			// tcf.write((args[0]+","+args[2*i+1]+","+args[2*i+2]+"\n").getBytes());
		}
		
		
		// Router r = new Router(rlfName, tcfName, local_port);
		
		try {
			Socket s = new Socket(ip, 1234);

			ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
			ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
			// System.out.println("Sending the Distance Vector " + dvnew + " to
			// Router " + r);
			// if (dvnew != null)
			if(args[args.length-1].equals("last")){
				rlfContent += ":last";
			}
			
			oos.writeObject(rlfContent);
			oos.writeObject(tcfContent);
			
			
			
			//String str = (String)ois.readObject();
			//System.out.println("str:"+str);
			oos.close();
			s.close();
		} catch (Exception e) {
			e.printStackTrace();
		}


	
		
		
		/*//close the bufferedOutputStream
		try {
			rlf.close();
			tcf.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
}

⌨️ 快捷键说明

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