ZSIK_Projekt/server/server.c

202 lines
5.9 KiB
C
Raw Normal View History

2019-11-24 12:10:33 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <dirent.h>
2019-11-24 12:10:33 +01:00
int main(int argc, char **argv)
{
2019-12-01 15:31:08 +01:00
2019-11-24 12:10:33 +01:00
const int port = 7332;
struct sockaddr_in myaddr, endpoint;
struct dirent **namelist;
2019-12-01 15:31:08 +01:00
char buff[512];
char name_file[512];
char names_files[512];
2019-12-01 15:31:08 +01:00
int sdsocket, sdconnection, addrlen, sdconnection2, n, x;
ssize_t count = 0;
2019-11-24 12:10:33 +01:00
sdsocket = socket(AF_INET, SOCK_STREAM, 0);
addrlen = sizeof(struct sockaddr_in);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(port);
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sdsocket, (struct sockaddr *)&myaddr, addrlen) < 0)
{
2019-12-01 15:31:08 +01:00
printf("bind() failed\n");
2019-11-24 12:10:33 +01:00
return 1;
}
if (listen(sdsocket, 10) < 0)
{
2019-12-01 15:31:08 +01:00
printf("listen() failed\n");
2019-11-24 12:10:33 +01:00
return 1;
}
while (1)
{
while ((sdconnection =
accept(sdsocket,
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
2019-11-27 14:21:20 +01:00
printf("CONNECT FROM : %s\n ", inet_ntoa(endpoint.sin_addr));
2019-12-01 15:31:08 +01:00
bzero(buff, 512);
recv(sdconnection, buff, 512, 0);
close(sdconnection);
2019-12-01 15:31:08 +01:00
if (strncmp("GET_ALL_FILES", buff, 13) == 0)
{
printf("Sending list of names files\n");
2019-11-27 14:21:20 +01:00
bzero(names_files, 512);
bzero(name_file, 512);
2019-12-08 00:05:17 +01:00
n = scandir("..", &namelist, NULL, alphasort);
if (n == -1)
{
perror("scandir");
exit(EXIT_FAILURE);
}
while (n--)
{
sprintf(name_file, "%s\n", namelist[n]->d_name);
strcat(names_files, name_file);
free(namelist[n]);
}
free(namelist);
while ((sdconnection2 =
accept(sdsocket,
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
send(sdconnection2, names_files, 512, 0);
close(sdconnection2);
2019-12-08 00:05:17 +01:00
break;
}
}
else if (strncmp("SEND", buff, 4) == 0)
{
ssize_t total = 0;
strncpy(name_file, buff + 5, 508);
printf("Name of file :%s\n", name_file);
printf("I'm receving a file: %s\n", buff);
FILE *file = fopen(name_file, "wb");
if (file == NULL)
{
printf("fopen() failed");
}
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));
ssize_t n;
char buff[4096] = {0};
while ((sdconnection2 =
accept(sdsocket,
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
2019-12-09 14:28:52 +01:00
while ((n = recv(sdconnection2, buff, 4096, 0)) > 0)
2019-12-08 00:05:17 +01:00
{
total += n;
if (n == -1)
{
printf("recv() failed");
}
if (fwrite(buff, sizeof(char), n, file) != n)
{
printf("fwrite() failed");
}
memset(buff, 0, 4096);
}
printf("Downloading succes, numer of bytes = %ld\n", total);
fclose(file);
close(sdconnection2);
break;
}
2019-11-27 14:21:20 +01:00
}
2019-12-01 15:31:08 +01:00
else if (strncmp("EXIT", buff, 4) == 0)
2019-11-27 14:21:20 +01:00
{
printf("Server shut down..\n");
2019-11-27 14:21:20 +01:00
close(sdsocket);
return 0;
2019-11-29 15:42:35 +01:00
}
2019-12-01 15:31:08 +01:00
else if (strncmp("FTP", buff, 3) == 0)
2019-11-29 15:42:35 +01:00
{
2019-11-29 15:42:35 +01:00
bzero(name_file, 512);
2019-12-01 15:31:08 +01:00
strncpy(name_file, buff + 4, 509);
printf("File to send :%s\n", name_file);
FILE *file = fopen(name_file, "rb");
if (file == NULL)
{
2019-12-01 15:31:08 +01:00
printf("fopen() failed");
}
2019-11-29 15:42:35 +01:00
while ((sdconnection2 =
accept(sdsocket,
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
char package[4096] = {0};
while ((x = fread(package, sizeof(char), 4096, file)) > 0)
{
count += x;
if (send(sdconnection2, package, x, 0) == -1)
{
2019-12-01 15:31:08 +01:00
printf("send() failed");
}
memset(package, 0, 4096);
}
2019-12-01 15:31:08 +01:00
printf("Send success, number of bytes: %ld\n", count);
2019-12-01 15:31:08 +01:00
fclose(file);
2019-11-29 15:42:35 +01:00
close(sdconnection2);
break;
}
}
else
{
2019-12-01 15:31:08 +01:00
printf("Wrong query: %s\n", buff);
while ((sdconnection2 =
accept(sdsocket,
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
2019-11-27 14:21:20 +01:00
2019-12-01 15:31:08 +01:00
send(sdconnection2, "Wrong query", 512, 0);
close(sdconnection2);
break;
}
}
}
}
2019-11-24 12:10:33 +01:00
close(sdsocket);
return 0;
2019-12-01 15:31:08 +01:00
}