/********************************************************
* Speaker Verification Implemented Security             *
* CA4 Project                                           *
* Written by: Ronan Crowley (97084603)                  *
*        and  Paul Connolly (97307599)                  *
********************************************************/

import java.io.*;
import java.net.*;

public class Sendfile  {
    public static final int port = 4000;

    /** Empty Constructor **/
    public static void Sendfile() {
    }

    /** The Send file method, returns a boolean of success/failure.
        @param host The hostname computer recieving (IP Address)
        @param filename The filename to receive
    **/
    boolean send (String host, String filename) {
        Socket sock;
        FileInputStream fis;
        BufferedOutputStream bos;
        byte[] b = new byte[4096];
        int i;

        try {
            fis = new FileInputStream(filename);    // fis will create the file 

            // Establish connection
            System.out.println("Opening connection to " + host + " on port " + port + ".");
            sock = new Socket(host, port);          
            bos = new BufferedOutputStream(sock.getOutputStream()); 
            System.out.println("Connected to host.\nSending file.");

            while((i = fis.read(b)) != -1) {
                System.out.print(".");  //Print . for each 4096 bytes recieved
                bos.write(b, 0, i);
            }

            //Close streams and write file !
            bos.flush();
            bos.close();
            fis.close();
            System.out.println("\nFile sent.");
            return true;
        } catch (Exception e) { 
            e.printStackTrace();
            System.out.println("File Transfer Error sending: "+filename);
            return false;
        }
    }
    
}