Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.75 KB | None | 0 0
  1. #include <sys/socket.h>
  2. #include <stdio.h>
  3. #include <netinet/in.h>
  4. #include <string.h>
  5. #include <arpa/inet.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8.  
  9. #define LENGTH 10000
  10.  
  11. int main(void) {
  12.     char message[1024];
  13.     struct sockaddr_in adr;
  14.     int gniazdo;
  15.     char separator[] = " ";
  16.     char * token;
  17.  
  18.     gniazdo = socket(PF_INET, SOCK_STREAM, 0);
  19.     adr.sin_family = AF_INET;
  20.     adr.sin_port = htons(3059);
  21.     adr.sin_addr.s_addr = inet_addr("150.254.79.243");
  22.     if (connect(gniazdo, (struct sockaddr*) &adr,
  23.                 sizeof(adr)) < 0)
  24.     {
  25.         printf("Nawiazanie polaczenia nie powiodlo sie.\n");
  26.         return 1;
  27.     }
  28.  
  29.     while(1) {
  30.     memset(message, 0, 1024);
  31.         printf("Polaczenie nawiazane.\nPodaj message: ");
  32.         fflush(stdout);
  33.         if (fgets(message,1024,stdin) != NULL)
  34.         {
  35.             printf("-------------------- ");
  36.         }
  37.  
  38.     token = strtok( message, separator );
  39.     printf("token: %s\n", token);
  40.  
  41.         if(strncmp(token, "send", 4) == 0) {
  42.         //send command
  43.         send(gniazdo, token, strlen(token), 0);
  44.         char* fs_name = "fileToSend.txt"; //to variable
  45.         char sdbuf[LENGTH];
  46.         printf("[Client] Sending %s to the Server...", fs_name);
  47.         //get file length
  48.         FILE *fileptr;
  49.         char *buffer;
  50.         long filelen;
  51.         fileptr = fopen(fs_name, "rb");  // Open the file in binary mode
  52.         fseek(fileptr, 0, SEEK_END);          // Jump to the end of the file
  53.         filelen = ftell(fileptr);             // Get the current byte offset in the file
  54.         printf("file length: %ld", filelen);
  55.         buffer = (char *)malloc((filelen+1)*sizeof(char));
  56.         //send file length     
  57.         send(gniazdo, buffer, strlen(buffer), 0);
  58.         fclose(fileptr); // Close the file
  59.  
  60.  
  61.         FILE *fs = fopen(fs_name, "r");
  62.         if(fs == NULL)
  63.         {
  64.             printf("ERROR: File %s not found.\n", fs_name);
  65.             exit(1);
  66.         }
  67.  
  68.         bzero(sdbuf, LENGTH);
  69.         int fs_block_sz;
  70.  
  71.         while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs))>0) {
  72.                 if(send(gniazdo, sdbuf, fs_block_sz, 0) < 0) {
  73.                         printf("ERROR: Failed to send file %s.\n", fs_name);
  74.                         break;
  75.                 }
  76.                 bzero(sdbuf, LENGTH);
  77.             }
  78.         printf("Ok File %s from Client was Sent!\n", fs_name);
  79.             printf("message wyslana (message sent).\n");
  80.         } else if(strncmp(token, "list", 4) == 0) {
  81.         int numOfPaths;
  82.         send(gniazdo, token, strlen(token), 0);
  83.         memset(message, '\0', 1024);
  84.         recv(gniazdo, message, 1024, 0);
  85.         sscanf(message, "%d", &numOfPaths);
  86.         for(int i = 0; i< numOfPaths; i++){
  87.         memset(message, '\0', 1024);
  88.         recv(gniazdo, message, 1024, 0);
  89.         printf("%s\n", message);
  90.     }
  91.         } else if(strncmp(token, "quit", 4) == 0) {
  92.             send(gniazdo, token, strlen(token), 0);
  93.             close(gniazdo);
  94.             exit(0);
  95.         } else {
  96.             printf("Nieznana komenda!\n");        
  97.         }
  98.     }
  99.     close(gniazdo);
  100.  
  101.  
  102.  
  103.     /* Receive File from Server */
  104.     /*printf("[Client] Receiveing file from Server and saving it as final.txt...");
  105.     char* fr_name = "/home/aryan/Desktop/progetto/final.txt"; //to variable
  106.     FILE *fr = fopen(fr_name, "a");
  107.     if(fr == NULL)
  108.         printf("File %s Cannot be opened.\n", fr_name);
  109.     else
  110.     {
  111.         bzero(revbuf, LENGTH);
  112.         int fr_block_sz = 0;
  113.         int success = 0;
  114.         //while(success == 0)
  115.         //{
  116.             while(fr_block_sz = recv(sockfd, revbuf, LENGTH, 0))
  117.             {
  118.                 if(fr_block_sz < 0)
  119.                 {
  120.                     error("Receive file error.\n");
  121.                 }
  122.                 int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
  123.                 if(write_sz < fr_block_sz)
  124.                 {
  125.                     error("File write failed.\n");
  126.                 }
  127.                 else if(fr_block_sz)
  128.                 {
  129.                     break;
  130.                 }
  131.                 bzero(revbuf, LENGTH);
  132.             }
  133.             printf("Ok received from server!\n");
  134.             success = 1;
  135.             fclose(fr);
  136.         //}
  137.     }*/
  138.     return 0;
  139.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement