Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include "lib.h"
- #include <stdlib.h>
- #include <dirent.h>
- #include <string.h>
- #define HOST "127.0.0.1"
- #define PORT 10001
- int main()
- {
- printf("[RECEIVER] Starting.\n");
- msg r;
- int i, res;
- char inc_msg[MSGSIZE];
- char* token;
- DIR* dirp;
- struct dirent * dp;
- int filecount, filesize;
- FILE * fp;
- init(HOST, PORT);
- for (i = 0; i < COUNT; i++) {
- /* wait for message */
- res = recv_message(&r);
- if (res < 0) {
- perror("[RECEIVER] Receive error. Exiting.\n");
- return -1;
- }
- strcpy(inc_msg, r.payload);
- token=strtok(inc_msg," ");
- //cd
- if(strcmp(token,"cd")==0)
- {
- token = strtok(NULL, " ");
- chdir(token);
- }
- //end cd
- //ls
- if(strcmp(token,"ls")==0)
- {
- token = strtok(NULL, " "); //extrage argumentul
- dirp = opendir(token);
- filecount=0;
- while ( (dp = readdir(dirp)) != NULL)
- {
- filecount++;
- }
- /* trimtie ACK */
- strcpy(r.payload, "ACK");
- r.len=strlen(r.payload) + 1;
- res = send_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Send ACK error. Exiting.\n");
- return -1;
- }
- /* trimite filecount */
- sprintf(r.payload, "%d", filecount);
- r.len=strlen(r.payload) + 1;
- res = send_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Send filecount error. Exiting.\n");
- return -1;
- }
- dirp=NULL;
- dirp = opendir(token);
- /* asteapta */
- res = recv_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Receive error. Exiting.\n");
- return -1;
- }
- while ( (dp = readdir(dirp)) != NULL)
- {
- /* trimite nume fisier */
- strcpy(r.payload, dp->d_name);
- r.len=strlen(r.payload) + 1;
- res = send_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Receive error. Exiting.\n");
- return -1;
- }
- /* asteapta */
- res = recv_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Receive error. Exiting.\n");
- return -1;
- }
- }
- (void)closedir(dirp);
- }
- //end ls
- //cp
- if(strcmp(token, "cp") == 0)
- {
- token = strtok(NULL, " ");
- fp = fopen(token, "rb");
- /* trimtie ACK */
- strcpy(r.payload, "ACK");
- r.len=strlen(r.payload) + 1;
- res = send_message(&r);
- /* afla filesize */
- fseek(fp, 0, SEEK_END);
- filesize = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA %d", filesize);
- /* trimite filesize */
- sprintf(r.payload, "%d", filesize);
- r.len=strlen(r.payload) + 1;
- res = send_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Send error. Exiting.\n");
- return -1;
- }
- char buffer[1400];
- /* asteapta */
- res = recv_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Receive error. Exiting.\n");
- return -1;
- }
- int bytesRead = 0;
- while((bytesRead = fread(r.payload, sizeof(char), 1400, fp)) != 0)
- {
- //memcpy(r.payload, buffer, bytesRead);
- r.len = bytesRead;
- res = send_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Send error. Exiting.\n");
- return -1;
- }
- /* asteapta */
- res = recv_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Receive error. Exiting.\n");
- return -1;
- }
- }
- fclose(fp);
- }
- //end cp
- //sn
- if(strcmp(token,"sn")==0)
- {
- token=strtok(NULL," ");
- /* primeste filesize */
- res = recv_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Receive error. Exiting.\n");
- return -1;
- }
- /* numarul de pachete in care va fi impartit fisierul */
- filesize = atoi(r.payload);
- int packs = filesize/1400+1;
- char * filename = malloc((5+strlen(token))*sizeof(char));
- strcpy(filename, "new_");
- strcat(filename, token);
- fp = fopen(filename, "w");
- for(i=0; i<packs; i++)
- {
- /* primeste pachet fisier */
- res=recv_message(&r);
- if (res < 0)
- {
- perror("[RECEIVER] Receive error. Exiting.\n");
- return -1;
- }
- /* pune pachetul in fisier */
- fputs(r.payload, fp);
- }
- fclose(fp);
- }
- //end sn
- // exit
- if(strcmp(token, "exit") == 0)
- {
- token = strtok(NULL, " ");
- if(strcmp(token, "exit") == 0);
- exit(0);
- }
- //end exit
- /* send dummy ACK */
- res = send_message(&r);
- if (res < 0) {
- perror("[RECEIVER] Send ACK error. Exiting.\n");
- return -1;
- }
- }
- printf("[RECEIVER] Finished receiving..\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment