Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- import java.util.Date;
- public class Receiver
- {
- static final int PORT_NUMBER = 42730; //port to connect on
- private DatagramSocket sock; //receiver socket
- private String hostfile; //output file
- private String targethost; //connecting host for receiver
- private int windowsize; //size of window
- private int timeout; //timeout in milliseconds
- private int payload; //size of payload in bytes
- public Receiver(String hostfile, String targethost, int windowsize, int timeout, int payload)
- {
- try
- {
- this.hostfile = hostfile;
- this.targethost = targethost;
- this.windowsize = windowsize;
- this.timeout = timeout;
- this.payload = payload;
- }
- catch(Exception e)
- {
- e.printStackTrace();
- }
- }
- public void run()
- {
- try
- {
- sock = new DatagramSocket(PORT_NUMBER);
- while ( true )
- {
- if ( sock != null )
- connected();
- }
- }
- catch (Exception e)
- {
- System.out.println("Error: " + e);
- }
- }
- private void connected()
- {
- try
- {
- System.out.println( "Got a connection: " +
- (new Date(new Date().getTime() + 7200000 )).toString());
- System.out.println("Connected to: " + targethost +
- " Port: " + PORT_NUMBER );
- int seqnum; //sequence number
- int length; //length of data
- byte[] data; //data
- byte[] hash; //checksum
- byte[] incData = new byte[payload];
- DatagramPacket dp = new DatagramPacket(incData, incData.length);
- sock.receive(dp);
- ByteArrayInputStream byteArray = new ByteArrayInputStream(dp.getData());
- DataInputStream dis = new DataInputStream(byteArray);
- seqnum = dis.readInt();
- length = dis.readInt();
- //data = dis.read(dp.getData(), 8, length);
- //hash = dis.read(dp.getData(), 8+length, 16);
- //extract seqnum from packet
- //if seqnum < base
- //ack packet
- //else if seqnum >= base+w
- //drop
- //if base<=seqnum<base+w
- //ack_packet(seqnum)
- //if seqnum=base
- //for i=seqnum;i<seqnum+w;i++
- //write packet to file(i)
- //base++
- //else
- //buffer packet(seqnum)
- }
- catch ( Exception ex )
- {
- System.out.println( "Error: " + ex );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement