Skip to content

Commit

Permalink
phase 3;
Browse files Browse the repository at this point in the history
no,
i am the one who knocks!
  • Loading branch information
moamdavoodi committed Jul 24, 2020
1 parent 65d75c3 commit 7403afc
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/java/server/FileDownloadClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package server;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class FileDownloadClient {
private static final int PORT = 9060;
private static final String IP = "127.0.0.1";
static DataInputStream in;
static DataOutputStream out;
static Scanner scanner;
private static Socket socket;

public static void main(String[] args) {
scanner = new Scanner(System.in);
String name = scanner.nextLine();
String path = scanner.nextLine();
try {
socket = new Socket(IP, PORT);
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
out.writeUTF("DownloadName:" + name);
out.flush();
int byteNumber = (int) Double.parseDouble(in.readUTF());
byte[] allBytes = new byte[byteNumber];
for (int i = 0; i < byteNumber; i++) {
allBytes[i] = in.readByte();
}
FileOutputStream fileOutputStream = new FileOutputStream(path);
fileOutputStream.write(allBytes);
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}
82 changes: 82 additions & 0 deletions src/main/java/server/FileServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;

public class FileServer {
public static final int PORT = 9060;
private static final String PATH = "src\\main\\resources\\";
private DataOutputStream out;
private DataInputStream in;
private ServerSocket fileServer;

public FileServer() throws IOException {
fileServer = new ServerSocket(PORT);
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Socket socket = fileServer.accept();
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
new main.java.FileServer.ClientHandler(out, in, socket).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}).start();
}

static class ClientHandler extends Thread {
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
private Socket socket;

private ClientHandler(DataOutputStream dataOutputStream, DataInputStream dataInputStream, Socket socket) {
this.dataOutputStream = dataOutputStream;
this.dataInputStream = dataInputStream;
this.socket = socket;
}

@Override
public void run() {
try {
String input;
while (true) {
input = dataInputStream.readUTF();
if (input.startsWith("DownloadName:")) {
int colonIndex = input.indexOf(":");
String fileName = input.substring(colonIndex + 1);
File file = new File(PATH + fileName);
byte[] fileContent = Files.readAllBytes(file.toPath());
dataOutputStream.writeUTF("" + fileContent.length);
dataOutputStream.flush();
for (byte b : fileContent) {
dataOutputStream.write(b);
dataOutputStream.flush();
}
return;
} else if (input.startsWith("UploadName:")) {
String[] parts = input.split(":");
String fileName = parts[1];
int fileByteSize = Integer.parseInt(parts[2]);
byte[] allBytes = new byte[fileByteSize];
for (int i = 0; i < fileByteSize; i++) {
allBytes[i] = dataInputStream.readByte();
}
FileOutputStream fileOutputStream = new FileOutputStream(PATH + fileName);
fileOutputStream.write(allBytes);
fileOutputStream.close();
return;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/server/FileUploadClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package server;

import java.io.*;
import java.net.Socket;
import java.nio.file.Files;
import java.util.Scanner;

public class FileUploadClient {
private static final int PORT = 9060;
private static final String IP = "127.0.0.1";
static DataInputStream in;
static DataOutputStream out;
static Scanner scanner;
private static Socket socket;


public static void main(String[] args) {
scanner = new Scanner(System.in);
String name = scanner.nextLine();
String location = scanner.nextLine();
try {
socket = new Socket(IP, PORT);
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
File file = new File(location + name);
byte[] fileContent = Files.readAllBytes(file.toPath());
out.writeUTF("UploadName:" + name + ":" + fileContent.length);
out.flush();
for (byte b : fileContent) {
out.write(b);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}

}
}

0 comments on commit 7403afc

Please sign in to comment.