//package experiment2;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.*; // we use Sockets
import java.nio.file.Paths;

public class FileTransferClientUDPjlibcnds {

    
    public static void main(String args[]) throws Exception{ 
        // Arguments: Server name & port & filename to transfer
        String srvName = args[0]; // server Name
        int srvPort = Integer.parseInt(args[1]); // server UDP port
        String filename = Paths.get(args[2]).toAbsolutePath().toString();

	    // Open special datagramm socket from jlibcnds library, do not change this
	    javax.net.DatagramSocket dtgSock;
        dtgSock = new javax.net.DatagramSocket();
        InetSocketAddress srvSockAddr = new InetSocketAddress(srvName, srvPort);
        dtgSock.connect(srvSockAddr);

        byte[] buf = new byte[8];

	    java.io.FileInputStream fr;
        try {
            fr = new java.io.FileInputStream(new File(filename));
            int len; // number of bytes written from the file

            while ((len=fr.read(buf,0,buf.length))!=-1){

                DatagramPacket packet = new DatagramPacket(buf, len);
                dtgSock.send(packet);
                System.out.print("*");
                Thread.sleep(100);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        }
        finally {
            DatagramPacket packet = new DatagramPacket(buf, 0); // Send an empty packet to the server to indicate end of file
            dtgSock.send(packet);
            dtgSock.close();	// Close the Socket

        }

    }
}