diff --git a/Client.java b/Client.java index 0aa937b..e9d59ea 100644 --- a/Client.java +++ b/Client.java @@ -1,6 +1,12 @@ import javax.swing.*; import java.io.*; import java.net.Socket; +import java.nio.file.Files; +import java.io.BufferedOutputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.BufferedInputStream; +import java.io.IOException; import java.util.Scanner; public class Client { @@ -18,7 +24,7 @@ public static void main(String args[]) { System.out.print("Enter The Server IP Address: "); String ip = kb.nextLine(); - + try { socket = new Socket(ip, port); @@ -30,12 +36,13 @@ public static void main(String args[]) { } catch(IOException ioe) { ioe.printStackTrace(); } + } /* * - * The getTransferFile() method takes the URL of the file to be transfered - * as String and then returns the URL as a String. + * The getTransferFile() method takes the URL of the file to be transfered as + * String and then returns the URL as a String. * */ private static String getTransferFile() { @@ -43,13 +50,23 @@ private static String getTransferFile() { JFileChooser fc = new JFileChooser(); int ans = fc.showOpenDialog(null); - if(ans == JFileChooser.APPROVE_OPTION) - { + if (ans == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile().getAbsolutePath(); + } return file; } + + //The getfileNameMethod gets us the filename so we can use it in the setUpStreams to send over the filename first . + + private static String getFileName(String filePath) { + File file = new File(filePath); + String fileName; + fileName = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("\\") + 1); + System.out.println(fileName); + return fileName; + } /* * @@ -57,9 +74,17 @@ private static String getTransferFile() { * BufferedOutputStream output. The p_input takes a FileInputStream pointing * */ - private static void setUpStreams(String file) throws IOException { - p_input = new BufferedInputStream(new FileInputStream(file), buffer); - output = new BufferedOutputStream(socket.getOutputStream(), buffer); + private static void setUpStreams(String fileName,String path) throws IOException { + + File file = new File(path); + output = new BufferedOutputStream(socket.getOutputStream()); + try (DataOutputStream d = new DataOutputStream(output)) { + d.writeUTF(fileName); + Files.copy(file.toPath(), d); + } + + + } private static void sendFileName(String file) throws IOException { @@ -69,9 +94,10 @@ private static void sendFileName(String file) throws IOException { output.flush(); } + private static void transferData() throws IOException { - while((n = p_input.read(b, 0, buffer)) != -1) { - output.write(b, 0, n); + while ((n = p_input.read(b, 0, buffer)) != -1) { + output.write(b, 0, n); } } diff --git a/Server.java b/Server.java index fc4fe6c..f48bca1 100644 --- a/Server.java +++ b/Server.java @@ -1,6 +1,12 @@ import java.io.*; import java.net.ServerSocket; import java.net.Socket; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.io.DataInputStream; +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.EOFException; import javax.swing.JFileChooser; public class Server { @@ -8,10 +14,9 @@ public class Server { private static int buffer = 16000000; private static ServerSocket server_socket; private static Socket socket; - private static BufferedOutputStream p_output; private static BufferedInputStream input; - private static byte[] b = new byte[buffer]; - private static int n; + + /* * @@ -30,7 +35,6 @@ public static void main(String args[]) { while(true) { waitForConnection(); setupStreams(); - transferData(); } } catch(EOFException eofe) { System.out.println("Error: " + eofe); @@ -61,6 +65,7 @@ private static void waitForConnection() throws IOException { System.out.println("Connected to " + host_name); } + /* * * Sets up the output and input streams of the server. @@ -74,11 +79,23 @@ private static void waitForConnection() throws IOException { * The flush() method flushes the output stream thus clearing * the output buffer in case there is some data left in the * buffer. There is no flush method for the input stream. + * + * I have changed this to define our own protocol using DataInput streams + * the client sends the file name and then the file we simply use Files.copy to copy this to the selected + * directory. saving a lot of code. * */ private static void setupStreams() throws IOException { + + System.out.println("Setting output stream..."); + String dir = getDirectory(); System.out.println("Output stream set\nSetting up input stream..."); input = new BufferedInputStream(socket.getInputStream(), buffer); + try (DataInputStream d = new DataInputStream(input)) { + String fileName = d.readUTF(); + System.out.println("Copying File "+fileName ); + Files.copy(d,Paths.get(dir, fileName)); + } System.out.println("Input stream set"); System.out.println("Setting Directory"); @@ -113,26 +130,7 @@ private static String readFilename() throws IOException { return filenameBuffer.toString("UTF-8"); } - /* - * - * The read() method takes data from BufferedInputStream input and writes - * it to the byte array b from element 0 to 16000000-1. The number of bytes - * read are stored in n. - * - * The write() method writes data from b to BufferedOutputStream output - * from position 0 to the number of bytes written. - * - * The write() method writes only till n as writting the whole array - * creates problem during the last iteration when all array elements are - * not reinitialized and the last few elemnts contain leftover values from - * the last iteration. - * - */ - private static void transferData() throws IOException { - while((n=input.read(b, 0, buffer)) != -1) { - p_output.write(b, 0, n); - } - } + /* * @@ -143,7 +141,6 @@ private static void transferData() throws IOException { private static void closeCrap() throws IOException { server_socket.close(); socket.close(); - p_output.close(); input.close(); }