#include #include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { const int port = 7332; const char ip[512] = "192.168.0.43"; struct sockaddr_in endpoint; struct dirent **namelist; char buff[512]; char name_file[512]; int sdsocket, addrlen, sdconnection, n, x; 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); close(sdsocket); 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; } 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 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 ((n = recv(sdsocket, buff, 4096, 0)) > 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) { 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"); } 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); close(sdsocket); goto start; } bzero(buff, 512); recv(sdsocket, buff, 512, 0); printf("%s\n", buff); } return 0; }