Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.Date;
  4.  
  5.  
  6. public class Receiver
  7. {
  8.     static final int PORT_NUMBER = 42730;   //port to connect on
  9.     private DatagramSocket sock;            //receiver socket
  10.     private String hostfile;                //output file
  11.     private String targethost;              //connecting host for receiver
  12.     private int windowsize;                 //size of window
  13.     private int timeout;                    //timeout in milliseconds
  14.     private int payload;                    //size of payload in bytes
  15.    
  16.     public Receiver(String hostfile, String targethost, int windowsize, int timeout, int payload)
  17.     {
  18.         try
  19.         {
  20.             this.hostfile = hostfile;
  21.             this.targethost = targethost;
  22.             this.windowsize = windowsize;
  23.             this.timeout = timeout;
  24.             this.payload = payload;
  25.         }
  26.         catch(Exception e)
  27.         {
  28.             e.printStackTrace();
  29.         }
  30.     }
  31.    
  32.     public void run()
  33.     {
  34.         try
  35.         {
  36.              sock = new DatagramSocket(PORT_NUMBER);
  37.              while ( true )
  38.              {        
  39.                  if ( sock != null )
  40.                      connected();
  41.              }         
  42.         }
  43.         catch (Exception e)
  44.         {
  45.             System.out.println("Error: " + e);
  46.         }
  47.     }
  48.    
  49.     private void connected()
  50.     {
  51.        try
  52.        {        
  53.           System.out.println( "Got a connection: " +
  54.                           (new Date(new Date().getTime() + 7200000 )).toString());
  55.           System.out.println("Connected to: " + targethost +
  56.                              "    Port: " + PORT_NUMBER );
  57.          
  58.           int seqnum;   //sequence number
  59.           int length;   //length of data
  60.           byte[] data;  //data
  61.           byte[] hash;  //checksum
  62.          
  63.           byte[] incData = new byte[payload];
  64.           DatagramPacket dp = new DatagramPacket(incData, incData.length);
  65.           sock.receive(dp);
  66.          
  67.           ByteArrayInputStream byteArray = new ByteArrayInputStream(dp.getData());
  68.           DataInputStream dis = new DataInputStream(byteArray);
  69.          
  70.           seqnum = dis.readInt();
  71.           length = dis.readInt();
  72.           //data = dis.read(dp.getData(), 8, length);
  73.           //hash = dis.read(dp.getData(), 8+length, 16);
  74.          
  75.           //extract seqnum from packet
  76.            
  77.             //if seqnum < base
  78.                 //ack packet
  79.             //else if seqnum >= base+w
  80.                 //drop
  81.             //if base<=seqnum<base+w
  82.                 //ack_packet(seqnum)
  83.                 //if seqnum=base
  84.                     //for i=seqnum;i<seqnum+w;i++
  85.                         //write packet to file(i)
  86.                         //base++
  87.             //else
  88.                 //buffer packet(seqnum)
  89.        }
  90.        catch ( Exception ex )
  91.        {
  92.           System.out.println( "Error: " + ex );
  93.        }        
  94.     }        
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement