sprzątanie

This commit is contained in:
s449273 2019-12-01 15:31:08 +01:00
parent d52c1fd55d
commit ff8022be18
2 changed files with 40 additions and 55 deletions

View File

@ -9,23 +9,15 @@
#include <netdb.h>
#include <sys/time.h>
int main(int argc, char **argv)
{
char odbior[10];
struct sockaddr_in endpoint;
int sdsocket, addrlen, sdconnection;
const int port = 7332;
const char ip[512] = "192.168.1.30";
char buff[512];
char eee[1];
struct sockaddr_in endpoint;
struct dirent **namelist;
int n;
char wiadomosc[512];
char mess[512];
char buff[512];
char name_file[512];
int sdsocket, addrlen, sdconnection, n;
endpoint.sin_family = AF_INET;
endpoint.sin_port = htons(port);
@ -35,8 +27,8 @@ int main(int argc, char **argv)
while (1)
{
start:
printf("Wpisz zapytanie do serwera\n");
printf("Enter the server query\n");
bzero(buff, 512);
scanf("%s", buff);
if (strncmp("CLIENTOFF", buff, 9) == 0)
@ -46,34 +38,35 @@ int main(int argc, char **argv)
}
else if (strncmp("HELP", buff, 4) == 0)
{
printf("COMMAND LIST:\n CLIENTOFF\n EXIT\n GET_ALL_FILES\n");
printf("COMMAND LIST:\n CLIENTOFF\n EXIT\n GET_ALL_FILES\n FTP_namefile\n");
goto start;
}
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket() nie powiodl sie (failed)\n");
printf("socket() failed\n");
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
printf("connect() nie powiodl sie (failed)\n");
printf("connect() failed\n");
return 0;
}
else
printf("CONNECTED\n");
send(sdsocket, buff, 512, 0);
close(sdsocket);
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("socket() nie powiodl sie (failed)\n");
printf("socket() failed\n");
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
printf("connect() nie powiodl sie (failed)\n");
printf("connect() failed\n");
return 0;
}
@ -81,18 +74,16 @@ int main(int argc, char **argv)
{
ssize_t total = 0;
strncpy(name_file, buff + 4, 509);
printf("dlugość nazwy:%ld\n", strlen(name_file));
printf("Plik do wyslania :%s\n", name_file);
printf("Name of file :%s\n", name_file);
FILE *file = fopen(name_file, "wb");
if (file == NULL)
{
printf("Nie udalo sie otowrzyc pliku");
printf("fopen() failed");
}
char addr[INET_ADDRSTRLEN];
printf("Pobieranie pliku: %s od %s\n", name_file, inet_ntop(AF_INET, &endpoint.sin_addr.s_addr, addr, INET_ADDRSTRLEN));
printf("Downloading file: %s od %s\n", name_file, inet_ntop(AF_INET, &endpoint.sin_addr.s_addr, addr, INET_ADDRSTRLEN));
ssize_t n;
char buff[4096] = {0};
@ -101,27 +92,26 @@ int main(int argc, char **argv)
total += n;
if (n == -1)
{
printf("recv Error");
printf("recv() failed");
}
if (fwrite(buff, sizeof(char), n, file) != n)
{
printf("fwrite Error");
printf("fwrite() failed");
}
memset(buff, 0, 4096);
}
printf("Przesylanie udane liczba bajtow = %ld\n", total);
printf("Downloading succes, numer of bytes = %ld\n", total);
fclose(file);
goto start;
}
bzero(odbior, 512);
recv(sdsocket, odbior, 512, 0);
printf("%s\n", odbior);
bzero(buff, 512);
recv(sdsocket, buff, 512, 0);
printf("%s\n", buff);
}
return 0;

View File

@ -9,17 +9,17 @@
#include <netdb.h>
#include <dirent.h>
ssize_t count = 0;
int main(int argc, char **argv)
{
const int port = 7332;
struct sockaddr_in myaddr, endpoint;
int sdsocket, sdconnection, addrlen, sdconnection2, n, rec;
char odbior[512];
struct dirent **namelist;
char buff[512];
char name_file[512];
char names_files[512];
int sdsocket, sdconnection, addrlen, sdconnection2, n, x;
ssize_t count = 0;
sdsocket = socket(AF_INET, SOCK_STREAM, 0);
addrlen = sizeof(struct sockaddr_in);
@ -30,13 +30,13 @@ int main(int argc, char **argv)
if (bind(sdsocket, (struct sockaddr *)&myaddr, addrlen) < 0)
{
printf("bind() nie powiodl sie (failed)\n");
printf("bind() failed\n");
return 1;
}
if (listen(sdsocket, 10) < 0)
{
printf("listen() nie powiodl sie (failed)\n");
printf("listen() failed\n");
return 1;
}
@ -50,12 +50,12 @@ int main(int argc, char **argv)
{
printf("CONNECT FROM : %s\n ", inet_ntoa(endpoint.sin_addr));
bzero(odbior, 512);
rec = recv(sdconnection, odbior, 512, 0);
bzero(buff, 512);
recv(sdconnection, buff, 512, 0);
close(sdconnection);
if (strncmp("GET_ALL_FILES", odbior, 13) == 0)
if (strncmp("GET_ALL_FILES", buff, 13) == 0)
{
printf("Sending list of names files\n");
@ -72,7 +72,6 @@ int main(int argc, char **argv)
while (n--)
{
sprintf(name_file, "%s\n", namelist[n]->d_name);
strcat(names_files, name_file);
free(namelist[n]);
@ -91,27 +90,24 @@ int main(int argc, char **argv)
break;
}
}
else if (strncmp("EXIT", odbior, 4) == 0)
else if (strncmp("EXIT", buff, 4) == 0)
{
printf("Server shut down..\n");
close(sdsocket);
return 0;
}
else if (strncmp("FTP", odbior, 3) == 0)
else if (strncmp("FTP", buff, 3) == 0)
{
bzero(name_file, 512);
strncpy(name_file, odbior + 4, 509);
printf("dlugość nazwy:%ld\n", strlen(name_file));
printf("Plik do wyslania :%s\n", name_file);
strncpy(name_file, buff + 4, 509);
printf("File to send :%s\n", name_file);
FILE *file = fopen(name_file, "rb");
if (file == NULL)
{
printf("Open file failed");
printf("fopen() failed");
}
while ((sdconnection2 =
@ -120,7 +116,6 @@ int main(int argc, char **argv)
&addrlen)) >= 0)
{
int x;
char package[4096] = {0};
while ((x = fread(package, sizeof(char), 4096, file)) > 0)
{
@ -128,28 +123,28 @@ int main(int argc, char **argv)
if (send(sdconnection2, package, x, 0) == -1)
{
printf("Send failed");
printf("send() failed");
}
memset(package, 0, 4096);
}
printf("Send Success, NumBytes = %ld\n", count);
fclose(file);
printf("Send success, number of bytes: %ld\n", count);
fclose(file);
close(sdconnection2);
break;
}
}
else
{
printf("Wrong query\n %s nie istnieje\n", odbior);
printf("Wrong query: %s\n", buff);
while ((sdconnection2 =
accept(sdsocket,
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
send(sdconnection2, "wrong query", 512, 0);
send(sdconnection2, "Wrong query", 512, 0);
close(sdconnection2);
break;
@ -159,4 +154,4 @@ int main(int argc, char **argv)
}
close(sdsocket);
return 0;
}
}