📄 server.java
字号:
package dsr;import java.net.*;import java.io.*;//this implements the server that listens for the DSR packets and inserts them into the//event queue which then handles the packets.public class Server implements Runnable { private DatagramSocket socket; private DatagramPacket packet; static final int MAXLEN = 1000; //the function that creates the socket an listens for the packets to be received public static void startServ(int port) throws IOException { if (!RouteTable.routingTable.isEmpty()){ RouteTableEntry tmpRoute = new RouteTableEntry((RouteTableEntry)RouteTable.routingTable.lastElement()); InetAddress tmpAd = tmpRoute.dstIP; //create a socket to receive datagrams on specified ports. System.out.println("the tmpAd is " + tmpAd.toString()); DatagramSocket socket = new DatagramSocket(port, tmpAd); //DatagramSocket socket = new DatagramSocket(port); DatagramPacket packet = new DatagramPacket(new byte[MAXLEN], MAXLEN); //for(int threadCT = 0; threadCT < 10; threadCT++) { Server agent = new Server(socket, packet); Thread thread = new Thread(agent); thread.start(); System.out.println("Created thread = " + thread.getName()); //} } else { System.out.println("there is no available network interface"); } } //constructor public Server (DatagramSocket socket, DatagramPacket packet) { this.socket = socket; this.packet = packet; } //the run function to handle the threads. //it receives the packet it calls handleConnection() to handle that. public void run() { for (;;) { try { RouteTable.PrintRouteTable(); System.out.println("waiting for next packet"); socket.receive(packet); handleConnection(socket, packet); } catch (IOException e) { System.out.println("Exception in run = " + e.getMessage()); } } } //the function that inserts the packet received into the event queue public void handleConnection(DatagramSocket socket, DatagramPacket packet) throws IOException { //the packet that has been received has to be appended to the event queue. //InterfaceList.findInterface() InetAddress srcIP = packet.getAddress(); InetAddress dstIP = socket.getLocalAddress(); System.out.println("the packet was received from" + srcIP.toString()); if (EventQueue.insertEventQueueEntry(packet, srcIP, dstIP)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -