154 lines
4.0 KiB
C
154 lines
4.0 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>
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
const int port = 7332;
|
|
const char ip[512] = "192.168.0.102";
|
|
struct sockaddr_in endpoint;
|
|
struct dirent **namelist;
|
|
char buff[512];
|
|
char name_file[512];
|
|
int sdsocket, addrlen, n, x, lenbyt;
|
|
|
|
endpoint.sin_family = AF_INET;
|
|
endpoint.sin_port = htons(port);
|
|
endpoint.sin_addr.s_addr = inet_addr(ip);
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
ssize_t count = 0;
|
|
|
|
while (1)
|
|
{
|
|
start:
|
|
printf("Enter the server query\n");
|
|
bzero(buff, 512);
|
|
scanf("%s", buff);
|
|
|
|
if (strncmp("CLIENTOFF", buff, 9) == 0)
|
|
{
|
|
close(sdsocket);
|
|
return 0;
|
|
}
|
|
else if (strncmp("HELP", buff, 4) == 0)
|
|
{
|
|
printf("COMMAND LIST:\n CLIENTOFF\n EXIT\n GET_ALL_FILES\n FTP_namefile\n SEND_namefile\n");
|
|
goto start;
|
|
}
|
|
|
|
if ((sdsocket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
|
{
|
|
printf("socket() failed\n");
|
|
return 1;
|
|
}
|
|
if (connect(sdsocket, (struct sockaddr *)&endpoint, addrlen) < 0)
|
|
{
|
|
printf("connect() failed\n");
|
|
return 0;
|
|
}
|
|
else
|
|
printf("CONNECTED\n");
|
|
|
|
send(sdsocket, buff, 512, 0);
|
|
|
|
if (strncmp("FTP", buff, 3) == 0)
|
|
{
|
|
ssize_t total = 0;
|
|
strncpy(name_file, buff + 4, 509);
|
|
printf("Name of file :%s\n", name_file);
|
|
|
|
FILE *file = fopen(name_file, "wb");
|
|
if (file == NULL)
|
|
{
|
|
printf("fopen() failed");
|
|
}
|
|
|
|
char length[512];
|
|
recv(sdsocket, length, 512, 0);
|
|
printf("Rozmiar pliku to: %s\n", length);
|
|
lenbyt = atoi(length);
|
|
printf("Rozmiar pliku w intach to: %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 buff[4096] = {0};
|
|
while (total < lenbyt)
|
|
{
|
|
n = recv(sdsocket, buff, 4096, 0);
|
|
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);
|
|
|
|
goto start;
|
|
}
|
|
if (strncmp("SEND", buff, 4) == 0)
|
|
{
|
|
ssize_t count = 0;
|
|
bzero(name_file, 512);
|
|
strncpy(name_file, buff + 5, 507);
|
|
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("Rozmiar pliku to: %ld\n", res);
|
|
char length[512];
|
|
sprintf(length, "%ld", res);
|
|
send(sdsocket, length, 512, 0);
|
|
|
|
rewind(file);
|
|
|
|
char package[4096] = {0};
|
|
while ((x = fread(package, sizeof(char), 4096, file)) > 0)
|
|
{
|
|
count += x;
|
|
|
|
if (send(sdsocket, package, x, 0) == -1)
|
|
{
|
|
printf("send() failed");
|
|
}
|
|
memset(package, 0, 4096);
|
|
}
|
|
|
|
printf("Send success, number of bytes: %ld\n", count);
|
|
|
|
fclose(file);
|
|
|
|
goto start;
|
|
}
|
|
|
|
bzero(buff, 512);
|
|
recv(sdsocket, buff, 512, 0);
|
|
printf("%s\n", buff);
|
|
}
|
|
|
|
return 0;
|
|
}
|