forked from s449303/sieci-projekt-1
Please enter the commit message for your changes. Lines starting
with '#' will be ignored, and an empty message aborts the commit. Committer: Michał Jankowski <s449303@labs.wmi.amu.edu.pl> On branch master Initial commit Changes to be committed: new file: filestorage-client/.gitignore new file: filestorage-client/CMakeLists.txt new file: filestorage-client/Makefile new file: filestorage-client/README.md new file: filestorage-client/main.c new file: filestorage-client/panobrazek.jpg new file: filestorage-server/.gitignore new file: filestorage-server/README.md new file: filestorage-server/src/Connection.java new file: filestorage-server/src/Server.java new file: filestorage-server/storage/basquiat.jpg
This commit is contained in:
commit
76bb7c79e2
3
filestorage-client/.gitignore
vendored
Normal file
3
filestorage-client/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.idea
|
||||||
|
cmake-build-debug
|
||||||
|
client
|
6
filestorage-client/CMakeLists.txt
Normal file
6
filestorage-client/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.13)
|
||||||
|
project(untitled C)
|
||||||
|
|
||||||
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
|
||||||
|
add_executable(untitled main.c)
|
2
filestorage-client/Makefile
Normal file
2
filestorage-client/Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
install:
|
||||||
|
gcc -o client main.c
|
15
filestorage-client/README.md
Normal file
15
filestorage-client/README.md
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
# FileStorage-Client
|
||||||
|
|
||||||
|
Instalacja rewelacja klienta
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make install
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./client <server-ip> <server-port>
|
||||||
|
```
|
123
filestorage-client/main.c
Normal file
123
filestorage-client/main.c
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
#include <sys/socket.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include<stdio.h>
|
||||||
|
#include<stdlib.h>
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
|
char ipAddress[512];
|
||||||
|
char buffer[BUFFER_SIZE];
|
||||||
|
char fileName[255];
|
||||||
|
uint32_t buffer_size;
|
||||||
|
struct sockaddr_in adr;
|
||||||
|
FILE *file;
|
||||||
|
unsigned int port;
|
||||||
|
int gniazdo;
|
||||||
|
long fileSize;
|
||||||
|
ssize_t bytesRead;
|
||||||
|
size_t nread;
|
||||||
|
|
||||||
|
if (argc >= 2) {
|
||||||
|
strcpy(ipAddress, argv[1]);
|
||||||
|
port = atoi(argv[2]);
|
||||||
|
} else {
|
||||||
|
printf("Przyklad: ./client <host-ip-addr> <port>");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
gniazdo = socket(PF_INET, SOCK_STREAM, 0);
|
||||||
|
adr.sin_family = AF_INET;
|
||||||
|
adr.sin_port = htons(port);
|
||||||
|
adr.sin_addr.s_addr = inet_addr(ipAddress);
|
||||||
|
|
||||||
|
if (connect(gniazdo, (struct sockaddr *) &adr,
|
||||||
|
sizeof(adr)) < 0) {
|
||||||
|
printf("You did smth wrong, nie moge sie polaczyc!\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Czesc! Ponizej masz liste dostpenych komend:\n"
|
||||||
|
"/ls - lista plikow\n"
|
||||||
|
"/rm <nazwa> - usun plik\n"
|
||||||
|
"/send <sciezka-do-pliku> - wyslij plik na dana sciezke\n"
|
||||||
|
"/exit - wyjdz\n" );
|
||||||
|
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
printf("Wpisz komende: ");
|
||||||
|
memset(buffer, 0, BUFFER_SIZE*sizeof(char));
|
||||||
|
|
||||||
|
//wczytywanie
|
||||||
|
fgets(buffer, 1024, stdin);
|
||||||
|
|
||||||
|
//wysylka na serwer
|
||||||
|
buffer_size = htonl(BUFFER_SIZE);
|
||||||
|
write(gniazdo, &buffer_size, sizeof(buffer_size));
|
||||||
|
send(gniazdo, buffer, BUFFER_SIZE, 0);
|
||||||
|
|
||||||
|
if (strncmp(buffer, "/exit", 5) == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (strncmp(buffer, "/send", 5) == 0) {
|
||||||
|
strncpy(fileName, buffer+6, 255);
|
||||||
|
strtok(fileName, "\n");
|
||||||
|
printf("Wysylam! '%s'\n", fileName);
|
||||||
|
file = fopen(fileName, "r");
|
||||||
|
if (file) {
|
||||||
|
// rozmiar pliku
|
||||||
|
fseek(file, 0 , SEEK_END);
|
||||||
|
fileSize = ftell(file);
|
||||||
|
fseek(file, 0 , SEEK_SET);// sluzy do pobierania poczatku pliku
|
||||||
|
printf("It has %ld bytes.\n", fileSize);
|
||||||
|
|
||||||
|
buffer_size = 0;
|
||||||
|
buffer_size = htonl((uint32_t)fileSize);
|
||||||
|
|
||||||
|
write(gniazdo, &buffer_size, sizeof(buffer_size));
|
||||||
|
|
||||||
|
memset(buffer, 0, BUFFER_SIZE*sizeof(char));
|
||||||
|
int progress = 0;
|
||||||
|
while ((nread = fread(buffer, 1, sizeof buffer, file)) > 0) {
|
||||||
|
progress += nread;
|
||||||
|
printf("\rSending %d/%ld",progress, fileSize);
|
||||||
|
send(gniazdo, buffer, nread, 0);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
if (ferror(file)) {
|
||||||
|
printf("Error with file!");
|
||||||
|
close(gniazdo);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
fclose(file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// odczytaj odpowiedz
|
||||||
|
printf("Odpowiedz: \n");
|
||||||
|
memset(buffer, 0, BUFFER_SIZE*sizeof(char));
|
||||||
|
buffer_size = 0;
|
||||||
|
recv(gniazdo, &buffer_size, 4, 0);
|
||||||
|
buffer_size = ntohl(buffer_size);
|
||||||
|
|
||||||
|
while (buffer_size > 0) {
|
||||||
|
bytesRead = recv(gniazdo, buffer, BUFFER_SIZE, 0);
|
||||||
|
if (-1 == bytesRead) {
|
||||||
|
printf("Blad podczas wczytywania z gniazda");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
printf("%s", buffer);
|
||||||
|
buffer_size -= bytesRead;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
close(gniazdo);
|
||||||
|
return 0;
|
||||||
|
}
|
BIN
filestorage-client/panobrazek.jpg
Normal file
BIN
filestorage-client/panobrazek.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 424 KiB |
3
filestorage-server/.gitignore
vendored
Normal file
3
filestorage-server/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.idea
|
||||||
|
FileStorageServer.iml
|
||||||
|
*/**/*.class
|
9
filestorage-server/README.md
Normal file
9
filestorage-server/README.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# FileStorage-Server
|
||||||
|
|
||||||
|
Szybki guide jak odpalić Serwer
|
||||||
|
|
||||||
|
```
|
||||||
|
cd src
|
||||||
|
javac Server.java
|
||||||
|
java Server 8001
|
||||||
|
```
|
121
filestorage-server/src/Connection.java
Normal file
121
filestorage-server/src/Connection.java
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
import java.io.*;
|
||||||
|
import java.nio.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.nio.file.StandardOpenOption;
|
||||||
|
import java.util.*;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
public class Connection implements Runnable {
|
||||||
|
|
||||||
|
private DataInputStream dis;
|
||||||
|
private DataOutputStream dos;
|
||||||
|
private Socket socket;
|
||||||
|
|
||||||
|
// konstruktor
|
||||||
|
public Connection(Socket socket) throws IOException {
|
||||||
|
this.dis = new DataInputStream(socket.getInputStream());
|
||||||
|
this.dos = new DataOutputStream(socket.getOutputStream());
|
||||||
|
this.socket = socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
StringBuilder tc;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
String outputMessage = "OK.";
|
||||||
|
String filename;
|
||||||
|
String received = "";
|
||||||
|
int bytesRead = 0;
|
||||||
|
boolean end = false;
|
||||||
|
byte[] messageByte = new byte[1024];
|
||||||
|
|
||||||
|
// wczytywanie wejscia
|
||||||
|
int bytesToRead = this.dis.readInt();
|
||||||
|
while (!end) {
|
||||||
|
bytesRead = this.dis.read(messageByte);
|
||||||
|
received += new String(messageByte, 0, bytesRead);
|
||||||
|
|
||||||
|
if (received.length() == bytesToRead) {
|
||||||
|
end = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// log
|
||||||
|
System.out.println(socket + " Input: " + received);
|
||||||
|
|
||||||
|
if (received.startsWith("/exit")) {
|
||||||
|
this.socket.close();
|
||||||
|
Server.connections.removeElement(this);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (received.startsWith("/send")) {
|
||||||
|
filename = Server.storageDir + "/" + received.substring(6).trim();
|
||||||
|
Path file = Paths.get(filename);
|
||||||
|
bytesToRead = this.dis.readInt();
|
||||||
|
System.out.println(this.socket + " wysyla plik "+filename+". Rozmiar: "+bytesToRead+" bajtow. ");
|
||||||
|
|
||||||
|
boolean first = true;
|
||||||
|
while (bytesToRead > 0) {
|
||||||
|
bytesRead = this.dis.read(messageByte);
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
Files.write(file,Arrays.copyOf(messageByte, bytesRead));
|
||||||
|
} else {
|
||||||
|
Files.write(file, Arrays.copyOf(messageByte, bytesRead), StandardOpenOption.APPEND);
|
||||||
|
}
|
||||||
|
bytesToRead -= bytesRead;
|
||||||
|
}
|
||||||
|
System.out.println(this.socket + " Zapisano plik.");
|
||||||
|
outputMessage = "Zapisano plik!.";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (received.startsWith("/ls")) {
|
||||||
|
File folder = new File(Server.storageDir);
|
||||||
|
File[] listOfFiles = folder.listFiles();
|
||||||
|
tc = new StringBuilder();
|
||||||
|
tc.append(String.format("Znaleziono %s plikow\n", listOfFiles.length));
|
||||||
|
for (File file : listOfFiles) {
|
||||||
|
if (file.isFile()) {
|
||||||
|
tc.append(file.getName()).append("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outputMessage = tc.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (received.startsWith("/rm")) {
|
||||||
|
filename = received.substring(4).trim();
|
||||||
|
File file = new File(Server.storageDir + "/" + filename);
|
||||||
|
|
||||||
|
if (file.delete()) {
|
||||||
|
outputMessage = String.format("Usunieto plik %s ", filename);
|
||||||
|
} else {
|
||||||
|
outputMessage = String.format("Blad przy usunieciu pliku %s", filename);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.dos.write(ByteBuffer.allocate(4).putInt(outputMessage.length()).array());
|
||||||
|
this.dos.write(outputMessage.getBytes());
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// closing resources
|
||||||
|
this.dis.close();
|
||||||
|
this.dos.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
44
filestorage-server/src/Server.java
Normal file
44
filestorage-server/src/Server.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
import java.net.*;
|
||||||
|
|
||||||
|
// Server class
|
||||||
|
public class Server {
|
||||||
|
|
||||||
|
final public static String storageDir = System.getProperty("user.dir") + "/../storage";
|
||||||
|
|
||||||
|
public static Vector<Connection> connections = new Vector<>();
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
|
||||||
|
int port;
|
||||||
|
|
||||||
|
if (args.length > 0) {
|
||||||
|
port = Integer.parseInt(args[0]);
|
||||||
|
} else {
|
||||||
|
System.out.println("Uzywam: java Server <port>");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ServerSocket serverSocket = new ServerSocket(port);
|
||||||
|
System.out.println("Start serwera na porcie "+port);
|
||||||
|
|
||||||
|
Socket socket;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
// Akceptowanie nadchodzacego zapytania
|
||||||
|
socket = serverSocket.accept();
|
||||||
|
|
||||||
|
System.out.println("Nowe polaczenie: " + socket);
|
||||||
|
|
||||||
|
Connection connection = new Connection(socket);
|
||||||
|
|
||||||
|
Thread thread = new Thread(connection);
|
||||||
|
|
||||||
|
connections.add(connection);
|
||||||
|
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
filestorage-server/storage/basquiat.jpg
Normal file
BIN
filestorage-server/storage/basquiat.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 424 KiB |
Loading…
Reference in New Issue
Block a user