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

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

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

    /** Empty Constructor **/
    public void Recievefile() {
    }

    /** The Recieve file method
        @param name = The filename to recieve.
    **/
    public void rec (String name) {
        ServerSocket ssock;
        Socket sock;
        FileOutputStream fos;
        BufferedInputStream bis;
        byte[] b = new byte[4096];
        int i;

        try {
            ssock = new ServerSocket(port);
            sock = ssock.accept();
            System.out.println("Client connected.\nReceiving file.");
            ssock.close();
            
            bis = new BufferedInputStream(sock.getInputStream());
            fos = new FileOutputStream(name);
            while ((i = bis.read(b)) != -1) {
                System.out.print(".");      //Print . for each 4096 bytes recieved
                fos.write(b, 0, i);
            }
            fos.flush();
            fos.close();
            bis.close();
            System.out.println("\nFile received.");
        } catch (Exception e) { 
            e.printStackTrace();
            System.out.println("\nFile "+name+" recieve failed");
        }
    }
}