ZSIK_Projekt/server/serverv2.c

207 lines
5.8 KiB
C

#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 <sys/time.h>
#include <dirent.h>
int main(int argc, char **argv)
{
const int port = 7332;
struct sockaddr_in myaddr, endpoint;
struct dirent **namelist;
char buff[512];
char name_file[512];
char names_files[512];
int sdsocket, sdconnection, addrlen, n, x, lenbyt, s;
connect:
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);
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)
{
printf("bind() failed\n");
return 1;
}
if (listen(sdsocket, 10) < 0)
{
printf("listen() failed\n");
return 1;
}
while (1)
{
while ((sdconnection =
accept(sdsocket,
(struct sockaddr *)&endpoint,
&addrlen)) >= 0)
{
start:
printf("CONNECT FROM : %s\n", inet_ntoa(endpoint.sin_addr));
bzero(buff, 512);
recv(sdconnection, buff, 512, 0);
if (strncmp("GET_ALL_FILES", buff, 13) == 0)
{
printf("Sending list of names files\n");
bzero(names_files, 512);
bzero(name_file, 512);
DIR *d = opendir(".");
struct dirent *enterDirectory;
if (d == NULL)
{
printf("Open error\n");
return EXIT_FAILURE;
}
while ((enterDirectory = readdir(d)) != NULL)
{
if (enterDirectory->d_type == DT_REG)
{
sprintf(name_file, "%s\n", enterDirectory->d_name);
strcat(names_files, name_file);
}
}
closedir(d);
if (s = send(sdconnection, names_files, 512, 0) == 512)
{
printf("Send success\n");
goto start;
}
}
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 length[512];
recv(sdconnection, length, 512, 0);
lenbyt = atoi(length);
printf("File size: %d\n", lenbyt);
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 package[4096] = {0};
while (total < lenbyt)
{
n = recv(sdconnection, package, 4096, 0);
total += n;
if (n == -1)
{
printf("recv() failed");
}
if (fwrite(package, sizeof(char), n, file) != n)
{
printf("fwrite() failed");
}
memset(package, 0, 4096);
}
printf("Downloading succes, numer of bytes = %ld\n", total);
fclose(file);
goto start;
}
else if (strncmp("EXIT", buff, 4) == 0)
{
printf("Server shut down..\n");
close(sdsocket);
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)
{
ssize_t count = 0;
bzero(name_file, 512);
strncpy(name_file, buff + 4, 509);
printf("File to send :%s\n", name_file);
FILE *file = fopen(name_file, "rb");
if (file == NULL)
{
printf("fopen() failed");
}
fseek(file, 0L, SEEK_END);
long int res = ftell(file);
printf("Size file: %ld\n", res);
char length[512];
sprintf(length, "%ld", res);
send(sdconnection, length, 512, 0);
rewind(file);
char package[4096] = {0};
while ((x = fread(package, sizeof(char), 4096, file)) > 0)
{
count += x;
if (send(sdconnection, package, x, 0) == -1)
{
printf("send() failed");
}
memset(package, 0, 4096);
}
printf("Send success, number of bytes: %ld\n", count);
fclose(file);
goto start;
}
else
{
printf("Wrong query: %s\n", buff);
send(sdconnection, "Wrong Query", 512, 0);
goto start;
}
}
}
close(sdsocket);
return 0;
}