add DISCONNECT and wrong query update

This commit is contained in:
s449273 2020-01-04 09:42:46 +01:00
parent 42e7d1b007
commit f3a6059f05

View File

@ -20,8 +20,12 @@ int main(int argc, char **argv)
char name_file[512]; char name_file[512];
char names_files[512]; char names_files[512];
int sdsocket, sdconnection, addrlen, n, x, lenbyt, s; int sdsocket, sdconnection, addrlen, n, x, lenbyt, s;
connect:
sdsocket = socket(AF_INET, SOCK_STREAM, 0); sdsocket = socket(AF_INET, SOCK_STREAM, 0);
if (setsockopt(sdsocket, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int)) < 0)
printf("setsockopt(SO_REUSEADDR) failed");
addrlen = sizeof(struct sockaddr_in); addrlen = sizeof(struct sockaddr_in);
myaddr.sin_family = AF_INET; myaddr.sin_family = AF_INET;
@ -66,7 +70,7 @@ int main(int argc, char **argv)
if (d == NULL) if (d == NULL)
{ {
printf("OPEN ERROR\n"); printf("Open error\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -84,7 +88,7 @@ int main(int argc, char **argv)
if (s = send(sdconnection, names_files, 512, 0) == 512) if (s = send(sdconnection, names_files, 512, 0) == 512)
{ {
printf("SEND SUCCESS\n"); printf("Send success\n");
goto start; goto start;
} }
} }
@ -104,31 +108,31 @@ int main(int argc, char **argv)
char length[512]; char length[512];
recv(sdconnection, length, 512, 0); recv(sdconnection, length, 512, 0);
printf("Rozmiar pliku to: %s\n", length);
lenbyt = atoi(length); lenbyt = atoi(length);
printf("Rozmiar pliku w intach to: %d\n", lenbyt); printf("File size: %d\n", lenbyt);
char addr[INET_ADDRSTRLEN]; char addr[INET_ADDRSTRLEN];
printf("Downloading file: %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; ssize_t n;
char buff[4096] = {0}; char package[4096] = {0};
while (total < lenbyt) while (total < lenbyt)
{ {
n = recv(sdconnection, buff, 4096, 0); n = recv(sdconnection, package, 4096, 0);
total += n; total += n;
if (n == -1) if (n == -1)
{ {
printf("recv() failed"); printf("recv() failed");
} }
if (fwrite(buff, sizeof(char), n, file) != n) if (fwrite(package, sizeof(char), n, file) != n)
{ {
printf("fwrite() failed"); printf("fwrite() failed");
} }
memset(buff, 0, 4096); memset(package, 0, 4096);
} }
printf("Downloading succes, numer of bytes = %ld\n", total); printf("Downloading succes, numer of bytes = %ld\n", total);
@ -142,6 +146,13 @@ int main(int argc, char **argv)
close(sdsocket); close(sdsocket);
return 0; return 0;
} }
else if (strncmp("DISCONNECT", buff, 4) == 0)
{
printf("Server disconnect\n");
close(sdconnection);
close(sdsocket);
goto connect;
}
else if (strncmp("FTP", buff, 3) == 0) else if (strncmp("FTP", buff, 3) == 0)
{ {
ssize_t count = 0; ssize_t count = 0;
@ -157,7 +168,7 @@ int main(int argc, char **argv)
fseek(file, 0L, SEEK_END); fseek(file, 0L, SEEK_END);
long int res = ftell(file); long int res = ftell(file);
printf("Rozmiar pliku to: %ld\n", res); printf("Size file: %ld\n", res);
char length[512]; char length[512];
sprintf(length, "%ld", res); sprintf(length, "%ld", res);
send(sdconnection, length, 512, 0); send(sdconnection, length, 512, 0);
@ -184,8 +195,8 @@ int main(int argc, char **argv)
else else
{ {
printf("Wrong query: %s\n", buff); printf("Wrong query: %s\n", buff);
send(sdconnection, "Wrong Query", 512, 0);
send(sdsocket, "Wrong query", 512, 0);
goto start; goto start;
} }
} }