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>
|
2019-11-25 12:54:10 +01:00
|
|
|
#include <dirent.h>
|
2019-11-24 12:10:33 +01:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
const int port = 7332;
|
2019-11-25 12:54:10 +01:00
|
|
|
struct sockaddr_in myaddr, endpoint;
|
|
|
|
int sdsocket, sdconnection, addrlen, sdconnection2, n;
|
|
|
|
char odbior[512];
|
|
|
|
struct dirent **namelist;
|
|
|
|
char name_file[512];
|
|
|
|
char names_files[512];
|
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)
|
|
|
|
{
|
|
|
|
printf("bind() nie powiodl sie (failed)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(sdsocket, 10) < 0)
|
|
|
|
{
|
|
|
|
printf("listen() nie powiodl sie (failed)\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((sdconnection =
|
|
|
|
accept(sdsocket,
|
|
|
|
(struct sockaddr *)&endpoint,
|
|
|
|
&addrlen)) >= 0)
|
|
|
|
{
|
2019-11-25 12:54:10 +01:00
|
|
|
printf("Connected\n");
|
2019-11-24 12:10:33 +01:00
|
|
|
send(sdconnection, "CONNECT", 7, 0);
|
2019-11-25 12:54:10 +01:00
|
|
|
|
2019-11-24 12:10:33 +01:00
|
|
|
close(sdconnection);
|
2019-11-25 12:54:10 +01:00
|
|
|
break;
|
2019-11-24 12:10:33 +01:00
|
|
|
}
|
|
|
|
|
2019-11-25 12:54:10 +01:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
|
|
|
|
while ((sdconnection =
|
|
|
|
accept(sdsocket,
|
|
|
|
(struct sockaddr *)&endpoint,
|
|
|
|
&addrlen)) >= 0)
|
|
|
|
{
|
|
|
|
bzero(odbior, 512);
|
|
|
|
recv(sdconnection, odbior, 512, 0);
|
|
|
|
|
|
|
|
close(sdconnection);
|
|
|
|
|
|
|
|
if (strncmp("GET_ALL_FILES", odbior, 13) == 0)
|
|
|
|
{
|
|
|
|
printf("Sending list of names files\n");
|
|
|
|
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);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Wrong query\n");
|
|
|
|
while ((sdconnection2 =
|
|
|
|
accept(sdsocket,
|
|
|
|
(struct sockaddr *)&endpoint,
|
|
|
|
&addrlen)) >= 0)
|
|
|
|
{
|
|
|
|
|
|
|
|
send(sdconnection2, "Wrong query", 512, 0);
|
|
|
|
|
|
|
|
close(sdconnection2);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 12:10:33 +01:00
|
|
|
close(sdsocket);
|
|
|
|
return 0;
|
|
|
|
}
|