By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

how to write an algorithm for send a file in linux useing c i've code for it pls help

P: 1
//this is a code to send a file from one system to another in linux useing udp methods can anyone help me with writting an algorithm for this i know i'm asking for more pls help me i'll b very greatful //
Expand|Select|Wrap|Line Numbers
  1. #include "check_file.c"
  2.  
  3. send_file(int sockfd,struct sockaddr_in servaddr)
  4. {
  5.     int ret,fd,size,ch,nread=0,mtu,ctr=0;
  6.     int period=0,t;
  7.     char filename[20];
  8.     DATA d;
  9.     time_t ct;
  10.  
  11.     d.ttl=htons(100);
  12.     d.flag=0;
  13. x:     system("clear");
  14.     system("setterm -bold on");
  15.     printf("\n\n\n");
  16.     printf("\tEnter name of the file : \t");
  17.     fflush(stdin);
  18.     scanf("%s",filename);
  19.  
  20.     if(check_file(filename)==-1)
  21.          goto x;
  22.     printf("\n\n\n");
  23.     printf("\tEnter period between each send:\t");
  24.     fflush(stdin);
  25.     scanf("%d",&period);
  26.  
  27.     printf("\n\n\n");
  28.     printf("\tEnter life time for packet[-1] to skip]\t");
  29.     fflush(stdin);
  30.     scanf("%d",&t);
  31.  
  32.     if(t==-1)
  33.     {
  34.         printf("\tDefault ttl(100Secs) set for packet\n");
  35.         d.ttl=htons(period);
  36.     }
  37.     else
  38.         d.ttl=htons(t);
  39.  
  40.     system("clear");    
  41.     printf("\n\n\n");
  42.     printf("\n\n\tEnter type of MTU to send\n");
  43.     printf("\n\n");
  44.     printf("\t1. Explicit MTU\n");
  45.     printf("\t2. Default MTU\n");
  46.  
  47.  
  48.     printf("\tEnter your choice:\t");
  49.     fflush(stdin);
  50.     scanf("%d",&ch);
  51.  
  52.     printf("\n\n\n");
  53.     if(ch==1)
  54.     {
  55.         printf("\tEnter value of MTU\t:\t");
  56.         fflush(stdin);
  57.         scanf("%d",&mtu);
  58.         if(mtu>MTU)
  59.             d.mtu=htons(MTU);
  60.         else
  61.             d.mtu=htons(mtu);
  62.     }
  63.     else    
  64.         d.mtu=htons(MTU);
  65.  
  66.  
  67.     system("clear");
  68.     printf("\n\n\n");
  69.  
  70.     system("setterm -bold   on");
  71.     printf("\t1. Send Data to STDOUT\n");
  72.     printf("\t2. Save Data to a file\n");
  73.     system("setterm -bold off");
  74.  
  75.     printf("\n\n\n");
  76.     printf("\tEnter your choice:\t");    
  77.     fflush(stdin);
  78.     scanf("%d",&d.disp);
  79.  
  80.     strcpy(d.filename,filename);
  81.  
  82.     fd=open(filename,0);
  83.     if(fd==-1)
  84.         perror("open");
  85.     size=lseek(fd,0L,2);
  86.     d.fsize=htons(size);
  87.  
  88.     lseek(fd,0L,0);
  89.     ret=read(fd,d.buff,ntohs(d.mtu));
  90.     d.bsize=htons(ret);
  91.  
  92.     //----sending current time
  93.     ct=time(&ct);
  94.     printf("%d.....current time\n",ct);
  95.     d.ct=htonl(ct);
  96.  
  97.  
  98.     //......sent
  99.  
  100.     sendto(sockfd,(void *)&d,sizeof(d),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
  101.  
  102.     system("clear");
  103.     printf("\n\n");
  104.     printf("\n\t[%d] bytes sent to server of [%d] block\n",ret,++ctr);
  105.     nread+=ret;
  106.     printf("%d is mtu and %d is blk size...\n",ntohs(d.mtu),ntohs(d.bsize));
  107.     while(ntohs(d.mtu)<size)
  108.     {
  109.         ret=read(fd,d.buff,ntohs(d.mtu));
  110.         d.bsize=htons(ret);    
  111.         sleep(period);
  112.  
  113.         //...sending current time
  114.         ct=time(&ct);
  115.         printf("%d .. is current time\n",ct);
  116.         d.ct=htonl(ct);
  117.         //....sent
  118.  
  119.         sendto(sockfd,(void *)&d,sizeof(d),0,
  120.             (struct sockaddr *)&servaddr,sizeof(servaddr));
  121.         printf("[%d] bytes send to server [%d] block\n",ret,++ctr);
  122.         nread+=ret;
  123.  
  124.         if(nread==size)
  125.         {
  126.             printf("\n\n\n");
  127.             printf("Sent Total of [%d] bytes to server\n",nread);
  128.             break;
  129.         }
  130.     }
  131.    }
Oct 15 '10 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.