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

📄 sendthread.java

📁 这是一个用Java编写的路由器模拟程序。可以利用计算机模拟路由器实现路由表的生成、转发等等功能。
💻 JAVA
字号:
import java.net.*;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.util.concurrent.*;
class SendThread extends Thread
{
    //The Router that spawned this thread
    int whoSend;			//发送包的路由器的ID
    int sendPort;
	Router rt;
    //The port on which this thread listens for Distance Vector packets
    HashSet<Integer> neighborPorts;

    public SendThread(Router r,int ws,int pt,HashSet<Integer> neb)
    {
	super("SendThread");
	rt=r;
	whoSend=ws;
	sendPort=pt;

	neighborPorts=new HashSet<Integer>();
	neighborPorts=neb;
	
	//start the thread
	start();
    }
    
    public void run()
    {
	//The socket for representing accepted connection
		/*try
		{
			TimeUnit.SECONDS.sleep(6);
		}
		catch(Exception sss)
		{
			sss.printStackTrace();
		}*/
		while(true)
		{
			//The output stream associated with the connection
			if((Router.activeRouter).contains(new Integer(whoSend)))			//如果要发送包的路由器是活动的
			{
				Iterator it=neighborPorts.iterator();
				//System.out.println("neighborPorts:"+neighborPorts);
				while(it.hasNext())
				{
					int port=((Integer)it.next()).intValue();
					///System.out.println("shuchu port"+port);
					try
					{
						//System.out.println("InetAddress.getLocalHost() is"+InetAddress.getLocalHost());
					 	Socket t=new Socket(InetAddress.getLocalHost(),port);
					 	//System.out.print("t  ");
						ObjectOutputStream oos = new ObjectOutputStream(t.getOutputStream());
						oos.writeObject(rt.getPackets());
						oos.close();
						t.close();
					}
					catch(Exception ee)
					{
						/*
						System.out.print("eeeeeeeeee");
						ee.printStackTrace();
						*/
					}
				}
			}
			try
			{
			TimeUnit.SECONDS.sleep(2);
			}
			catch(Exception ex)
			{
				ex.printStackTrace();
			}
			if(Router.DataFlag)//如果有数据包要发送
			{
				HashMap<Integer,String> routingTable = rt.getRoutingTable();
				DataPacket datapacket=rt.getDataPacket();
				Integer dataDes=datapacket.getDes();
				String dataDV = routingTable.get(dataDes);
				String [] dataFirstDes=dataDV.split(" ");
				Integer firstDes=new Integer(dataFirstDes[0]);
				Integer firstDesPort=(Router.revrelation).get(firstDes);
				try
					{
						Socket s=new Socket(InetAddress.getLocalHost(),firstDesPort.intValue());
						
						ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
						oos.writeObject(datapacket);
						oos.close();
						s.close();
					}
					catch(Exception ee)
					{
						/*
						System.out.print("eeeeeeeeee");
						ee.printStackTrace();
						*/
					}
				
				Router.DataFlag=false;//重新赋值
			}
		}
	}
}

⌨️ 快捷键说明

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