client.java

来自「DSr project for the java applications」· Java 代码 · 共 117 行

JAVA
117
字号
package dsr;//this is an client that basically initiates the DSR application.//the logic followed here is, the client wants to send a data packet to some destination//if there is route to the destination in the routing table, it sends the packet directly,//otherwise it asks the DSR application to find a route to that destinationimport java.net.*;import java.io.*;import java.util.*;import java.lang.*;public class Client {  private static final int TIMEOUT = 3000;  private static final int MAXTRIES = 5;//initialises the various queues and tables.  public static TimerQueue timerQueue = new TimerQueue();  public static FloodIDQueue floodQueue = new FloodIDQueue();  public static EventQueue eventQueue = new EventQueue();  public static RouteTable routeTable = new RouteTable();  //public static NeighborList neighborList;  public static InterfaceList intQueue = new InterfaceList();  public static void main (String[] args) throws IOException {    if ((args.length < 1) || (args.length > 2))      throw new IllegalArgumentException("Parameters: <Server> <word> [<port>]");    //get the local address, uses the last adddress in the interface table as the local address    InetAddress localAddress =        ((InterfaceListEntry)InterfaceList.interfaceList.lastElement()).ipAddress;    //gets the server address from the command line address.    InetAddress serverAddress = InetAddress.getByName(args[0]);    System.out.println("the serveraddress is " + serverAddress.getHostAddress().toString());    System.out.println("the localaddress is " + localAddress.getHostAddress().toString());    //Printing out the routing table    RouteTable.PrintRouteTable();    int echoServPort = 0;   //Echo server port    if (args.length == 2)      echoServPort = Integer.parseInt(args[1]);    else      System.out.println("Please enter a port where the packet is to be sent");    //Create a UDP socket for sending and receiving.    DatagramSocket socket = new DatagramSocket(echoServPort, localAddress);    //set the maximum time to wait on a receive (milliseconds)    socket.setSoTimeout(TIMEOUT);    RREQPacket rreqPacket = new RREQPacket();    InetAddress nextHopAddress = InetAddress.getByName("0.0.0.0");    //RouteTableEntry tmpEntry = new RouteTableEntry();    int i = RouteTable.FindRouteTableEntry(serverAddress);    if (i == -1){      //entry not found for that destination      //generate an RREQ and do a local broadcast      RREQ.genRREQ(localAddress, serverAddress);      return;    }    else {      //there is an entry in the routing table for that destination      //so use that destination and send the packet      //here the packet sent is a data packet.      //for the time being let it be an RREQ packet with type as 3 and IP addresss 111.111.111.111      InetAddress tempIP = InetAddress.getByName("111.111.111.111");      rreqPacket = new RREQPacket((byte)3, (short)3, (byte)4, tempIP, 1000, tempIP, 1000);      //nextHopAddress = tmpEntry.nextHop;      nextHopAddress = (InetAddress)RouteTable.routingTable.elementAt(i);    }    //Convert the arguement string to bytes via the default encoding    //System.out.println("first time : " + rreqPacket.toString());    byte[] bytesToSend = RREQPacket.encode(rreqPacket);    //Create a datagram to send    DatagramPacket sendPacket = new DatagramPacket(bytesToSend, bytesToSend.length,        nextHopAddress, echoServPort);    System.out.println("data sent to add " + nextHopAddress.toString());    //Create a datagram paket to recieve    DatagramPacket recvPacket = new DatagramPacket(new byte[bytesToSend.length],        bytesToSend.length);    //packets may be lost so we need to keep trying    int tries = 0;                      //number of tries to receive a packet    boolean receivedResponse = false;   //true if packet was received    do {      socket.send(sendPacket);          //send the echo string      try {        socket.receive(recvPacket);     //Attempt to receive packet        System.out.println("the packet is received from " + recvPacket.getAddress().toString());        //if (!recvPacket.getAddress().equals(serverAddress))          //throw new IOException("Received packet from an unknown source");        receivedResponse = true;      } catch (InterruptedIOException e) {        tries += 1;        System.out.println("Timed Out, " + (MAXTRIES - tries) + "more tries...");      }    } while ((!receivedResponse) && (tries < MAXTRIES));    if (receivedResponse) {      rreqPacket = rreqPacket.decode(recvPacket);      System.out.println("the packet type is " + rreqPacket.type);      System.out.println("\n the received packect is " + rreqPacket.toString());    }    else      System.out.println("No response -- giving up");    socket.close();  }}

⌨️ 快捷键说明

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