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

📄 routerthread.java

📁 这是一个用Java编写的路由器模拟程序。可以利用计算机模拟路由器实现路由表的生成、转发等等功能。
💻 JAVA
字号:
import java.net.*;
import java.util.*;
import java.io.*;
import java.lang.*;
class RouterThread extends Thread
{
    //The Router that spawned this thread
    Router r;

    //The port on which this thread listens for Distance Vector packets
    int port;
	
	Packets pks;

	boolean recieve=true;		//是接受包还是发送包,默认:发送

	HashSet<String> neighborPorts;

    /* Constructor
     *
     * @r: the instance of the Router that spawned this thread
     * @port: the port number
     */
    public RouterThread(Router inrouter, int inport,boolean inrecieve)
    {
	//create a new thread
	super("Router Thread");

	r = inrouter;
	port = inport;
	recieve=inrecieve;
	
	pks=r.makePackets();
	
	neighborPorts=new HashSet<String>();
	neighborPorts=r.getNeighborPorts();
	
	//start the thread
	start();
    }

    /* Method Name: run
     *
     * Use: To start the execution of the thread
     */
    public void run()
    {
	//The socket used to listen on the port
	ServerSocket l = null;

	//The socket for representing accepted connection
	Socket t=null;

	//The input stream associated with the connection
	if(recieve==true)		//如果是接受包
	{
		ObjectInputStream ois;
		try
	    {
		//listen on the port
		l = new ServerSocket(port);
		//System.out.println("RouterThread: Listening for neighbors' updates on port " + port);
	    }
		catch(Exception e)
	    {
		System.out.println("RouterThread: Unable to open a listening socket on port: " + port);
		System.out.println("Quitting.");
		System.exit(1);
	    }
	    while(true)
	    {
		try
		    {
			//accept a connection
			t = l.accept();
		    }
		catch(Exception e)
		    {
			System.out.println("RouterThread: An I/O error occired while accepting a connection");
			System.exit(1);
		    }
		try
		    {
			//open the input stream
			ois = new ObjectInputStream(t.getInputStream());
			//oos = new ObjectOutputStream(t.getOutputStream());
			//read the distance vector packet
			Packets p = (Packets)ois.readObject();
			r.processDV(p);						//这里代码要改写!!!!!!!!!!!!!!!!!!!!!

			//process the packet
			///oos.write_value()												///哇~!!!!!!!!
			//close the stream and the connection
			ois.close();
			//oos.close();
			t.close();
		    }
		catch(Exception e)
		    {
			System.out.println("RouterThread: An I/O error occured while communicating");
			continue;
		    }
	    }
	}
	else					//如果是发送包
	{
		ObjectOutputStream oos;
		
		try
	    {
		//listen on the port
		l = new ServerSocket(port);
		//System.out.println("RouterThread: Listening for neighbors' updates on port " + port);
	    }
	catch(Exception e)
	    {
		System.out.println("RouterThread: Unable to open a listening socket on port: " + port);
		System.out.println("Quitting.");
		System.exit(1);
	    }

	while(true)
	    {
		try
		    {
			//accept a connection
			t = l.accept();
		    }
		catch(Exception e)
		    {
			System.out.println("RouterThread: An I/O error occired while accepting a connection");
			System.exit(1);
		    }
		try
		    {
			//open the input stream
			oos = new ObjectOutputStream(t.getOutputStream());
			//read the distance vector packet
			oos.writeObject(pks);
			/*
			Iterator ite=(pks.getPackets()).iterator();
			while(ite.hasNext())
			{
				oos.writeObject((Packet)ite.next());
			}*/
			//process the packet
			///oos.write_value()												///哇~!!!!!!!!
			//close the stream and the connection
			oos.close();
			t.close();
		    }
		catch(Exception e)
		    {
			System.out.println("RouterThread: An I/O error occured while communicating");
			continue;
		    }
	    }
	}
    }
}

⌨️ 快捷键说明

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