sieci-projekt-1/filestorage-client/main.c
Michał Jankowski 76bb7c79e2 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
2019-12-14 10:01:22 +01:00

124 lines
3.5 KiB
C

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