Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 36 additions & 10 deletions Client.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);

Expand All @@ -30,36 +36,55 @@ 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() {
String file = "";

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;
}

/*
*
* The setUpStreams() method sets up the BufferedInputStream p_input and
* 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 {
Expand All @@ -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);
}
}

Expand Down
47 changes: 22 additions & 25 deletions Server.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
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 {

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;



/*
*
Expand All @@ -30,7 +35,6 @@ public static void main(String args[]) {
while(true) {
waitForConnection();
setupStreams();
transferData();
}
} catch(EOFException eofe) {
System.out.println("Error: " + eofe);
Expand Down Expand Up @@ -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.
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
}


/*
*
Expand All @@ -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();
}

Expand Down