ZSIK_Projekt/server/send.c

119 lines
2.9 KiB
C
Raw Normal View History

#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>
int main(int argc, char **argv)
{
const int port = 7332;
const char ip[512] = "192.168.1.30";
2019-12-01 15:31:08 +01:00
struct sockaddr_in endpoint;
struct dirent **namelist;
2019-12-01 15:31:08 +01:00
char buff[512];
char name_file[512];
2019-12-01 15:31:08 +01:00
int sdsocket, addrlen, sdconnection, n;
endpoint.sin_family = AF_INET;
endpoint.sin_port = htons(port);
endpoint.sin_addr.s_addr = inet_addr(ip);
addrlen = sizeof(struct sockaddr_in);
while (1)
{
2019-11-27 14:21:20 +01:00
start:
2019-12-01 15:31:08 +01:00
printf("Enter the server query\n");
bzero(buff, 512);
2019-11-27 14:21:20 +01:00
scanf("%s", buff);
2019-11-27 14:21:20 +01:00
if (strncmp("CLIENTOFF", buff, 9) == 0)
{
close(sdsocket);
2019-11-27 14:21:20 +01:00
return 0;
}
else if (strncmp("HELP", buff, 4) == 0)
{
2019-12-01 15:31:08 +01:00
printf("COMMAND LIST:\n CLIENTOFF\n EXIT\n GET_ALL_FILES\n FTP_namefile\n");
2019-11-27 14:21:20 +01:00
goto start;
}
2019-11-27 14:21:20 +01:00
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
2019-12-01 15:31:08 +01:00
printf("socket() failed\n");
2019-11-27 14:21:20 +01:00
return 1;
}
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
2019-12-01 15:31:08 +01:00
printf("connect() failed\n");
2019-11-27 14:21:20 +01:00
return 0;
}
else
printf("CONNECTED\n");
2019-11-27 14:21:20 +01:00
send(sdsocket, buff, 512, 0);
2019-12-01 15:31:08 +01:00
2019-11-27 14:21:20 +01:00
close(sdsocket);
2019-11-27 14:21:20 +01:00
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
2019-12-01 15:31:08 +01:00
printf("socket() failed\n");
2019-11-27 14:21:20 +01:00
return 1;
}
2019-11-27 14:21:20 +01:00
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
{
2019-12-01 15:31:08 +01:00
printf("connect() failed\n");
2019-11-27 14:21:20 +01:00
return 0;
}
if (strncmp("FTP", buff, 3) == 0)
{
ssize_t total = 0;
strncpy(name_file, buff + 4, 509);
2019-12-01 15:31:08 +01:00
printf("Name of file :%s\n", name_file);
FILE *file = fopen(name_file, "wb");
if (file == NULL)
{
2019-12-01 15:31:08 +01:00
printf("fopen() failed");
}
char addr[INET_ADDRSTRLEN];
2019-12-01 15:31:08 +01:00
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 ((n = recv(sdsocket, buff, 4096, 0)) > 0)
{
total += n;
if (n == -1)
{
2019-12-01 15:31:08 +01:00
printf("recv() failed");
}
if (fwrite(buff, sizeof(char), n, file) != n)
{
2019-12-01 15:31:08 +01:00
printf("fwrite() failed");
}
memset(buff, 0, 4096);
}
2019-12-01 15:31:08 +01:00
printf("Downloading succes, numer of bytes = %ld\n", total);
fclose(file);
goto start;
}
2019-12-01 15:31:08 +01:00
bzero(buff, 512);
recv(sdsocket, buff, 512, 0);
printf("%s\n", buff);
}
return 0;
}