public static void main(String[] args) throws Exception{ 
		
		String filename =  null;
		int listening_port = 8888;
		InetAddress remote_IP = null;
		int remote_port = 9999;
		String log_filename = null;
		
		int MAXBYTE = 557;
		
		//make sure user is using program correctly
		if(args.length < 5){
			System.out.println("usage: java Reciever <filename> <listening_port> <remote_ip> <remote_port> <log_filename>");
			return;
		}else{
			try{
				filename = new String(args[0]);
				listening_port = Integer.parseInt(args[1]);
				remote_IP = InetAddress.getByName(args[2]);
				remote_port = Integer.parseInt(args[3]);
				log_filename = new String(args[4]);
				
			}catch (Exception e){
				System.err.println("Make sure inputs are valid");
			}
		}
		
		//initializing listening socket
		DatagramSocket dsock = new DatagramSocket(listening_port);
		
		//first 20 bytes are for checksum; MaxSize at 576
		byte arr1[] = new byte[MAXBYTE+20];
		DatagramPacket dpack = new DatagramPacket(arr1, arr1.length);
		
		int currentSeq = 0;
		boolean lastPacketReceived = false;
		
		//starting to receive
		FileOutputStream fos = new FileOutputStream(filename);
		PrintWriter toLog = null;
		if(!log_filename.contentEquals("stdout"))
			toLog = new PrintWriter(log_filename);
		
		while(!lastPacketReceived) { 
			
			dsock.receive(dpack);
			//System.out.println("Received " + dpack.getPort());  
			byte fileByte[] = dpack.getData(); 
			
			int packSize = Sender.getDataLength(fileByte);
			int sequence_number = Sender.getSequenceNumber(fileByte);
			short window_size = Sender.getWindowSize(fileByte);
			
			//if sequence_number = currentSeq && !packet.isCorrupt
			//write to file, send ack
			if(currentSeq == sequence_number && !Sender.isCorrupt(fileByte)){
				//write to file
				fos.write(Sender.getData(fileByte));
				
				//send ack
				byte ackByte[] = new byte[20];
				Sender.addHeader(ackByte, listening_port, remote_port, 0, sequence_number, 0, window_size, 0);
				DatagramPacket rpack = new DatagramPacket(ackByte, ackByte.length, remote_IP, remote_port);
				dsock.send(rpack);
				
				//increment currentSeq
				currentSeq++;
				
				if(Sender.getFin(fileByte) != 0){
					lastPacketReceived = true;
					fos.close();
					
					if(toLog != null)
						toLog.close();
	
					System.out.println("Delivery completed successfully");
				}
			}
		
				//write to log file
				if(toLog != null)
					toLog.println(Sender.toString(fileByte));
				else
					System.out.println(Sender.toString(fileByte));
		}
	}