?? routerthread.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; //是接受包還是發(fā)送包,默認(rèn):發(fā)送
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 //如果是發(fā)送包
{
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;
}
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -