Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <sys/wait.h>
  8. #include <sys/socket.h>
  9. #include <signal.h>
  10. #include <ctype.h>          
  11. #include <arpa/inet.h>
  12. #include <netdb.h>
  13. #include <unistd.h>
  14.  
  15. #define PORT 2002
  16. #define LENGTH 512
  17.  
  18.  
  19. void error(const char *msg)
  20. {
  21.     perror(msg);
  22.     exit(1);
  23. }
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27.  
  28.     printf("ddd");
  29.     /* Variable Definition */
  30.     int sockfd;
  31.     int nsockfd;
  32.     char revbuf[LENGTH];
  33.     struct sockaddr_in remote_addr;
  34.  
  35.     /* Get the Socket file descriptor */
  36.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  37.     {
  38.         error("ERROR: Failed to obtain Socket Descriptor!\n");
  39.     }
  40.  
  41.     /* Fill the socket address struct */
  42.     remote_addr.sin_family = AF_INET;
  43.     remote_addr.sin_port = htons(PORT);
  44.     remote_addr.sin_addr.s_addr = inet_addr("150.254.79.251");
  45.     bzero(&(remote_addr.sin_zero), 8);
  46.     printf("ddddd");
  47.     /* Try to connect the remote */
  48.     if (connect(sockfd, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1)
  49.     {
  50.         error("ERROR: Failed to connect to the host!\n");
  51.     }
  52.     else {
  53.         printf("[Client] Connected to server at port %d...ok!\n", PORT);
  54.     }
  55.    // while(1) {
  56.         //czyść bufor memset
  57.         //czytaj komendy
  58.         //podzial na tokeny strtok
  59.         //if pierwszy token to "send" to wywołaj f send()
  60.         //else if pierwszy "listFiles" to wywolaj f
  61.         //dopóki wczytany string != "QUIT"
  62.         //a na "QUIT" break i zamknięcie połączenia
  63.     //}
  64.  
  65.     /* Send File to Server */
  66.     //if(!fork())
  67.     //{
  68.         /*char* fs_name = "/home/aryan/Desktop/quotidiani.txt"; //to variable
  69.         char sdbuf[LENGTH];
  70.         printf("[Client] Sending %s to the Server...", fs_name);
  71.         FILE *fs = fopen(fs_name, "r");
  72.         if(fs == NULL)
  73.         {
  74.             printf("ERROR: File %s not found.\n", fs_name);
  75.             exit(1);
  76.         }
  77.  
  78.         bzero(sdbuf, LENGTH);
  79.         int fs_block_sz;
  80.         //int success = 0;
  81.         while((fs_block_sz = fread(sdbuf, sizeof(char), LENGTH, fs))>0)
  82.         {
  83.             if(send(sockfd, sdbuf, fs_block_sz, 0) < 0)
  84.             {
  85.                 printf("ERROR: Failed to send file %s.\n", fs_name);
  86.                 break;
  87.             }
  88.             bzero(sdbuf, LENGTH);
  89.         }
  90.         printf("Ok File %s from Client was Sent!\n", fs_name);
  91.         //success = 1;
  92.     //}
  93.  
  94.     /* Receive File from Server */
  95.     /*printf("[Client] Receiveing file from Server and saving it as final.txt...");
  96.     char* fr_name = "/home/aryan/Desktop/progetto/final.txt"; //to variable
  97.     FILE *fr = fopen(fr_name, "a");
  98.     if(fr == NULL)
  99.         printf("File %s Cannot be opened.\n", fr_name);
  100.     else
  101.     {
  102.         bzero(revbuf, LENGTH);
  103.         int fr_block_sz = 0;
  104.         int success = 0;
  105.         //while(success == 0)
  106.         //{
  107.             while(fr_block_sz = recv(sockfd, revbuf, LENGTH, 0))
  108.             {
  109.                 if(fr_block_sz < 0)
  110.                 {
  111.                     error("Receive file error.\n");
  112.                 }
  113.                 int write_sz = fwrite(revbuf, sizeof(char), fr_block_sz, fr);
  114.                 if(write_sz < fr_block_sz)
  115.                 {
  116.                     error("File write failed.\n");
  117.                 }
  118.                 else if(fr_block_sz)
  119.                 {
  120.                     break;
  121.                 }
  122.                 bzero(revbuf, LENGTH);
  123.             }
  124.             printf("Ok received from server!\n");
  125.             success = 1;
  126.             fclose(fr);
  127.         //}
  128.     }*/
  129.     close (sockfd);
  130.     printf("[Client] Connection lost.\n");
  131.     return (0);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement