?? sendthread.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; //發(fā)送包的路由器的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))) //如果要發(fā)送包的路由器是活動的
{
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)//如果有數(shù)據(jù)包要發(fā)送
{
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;//重新賦值
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -