473,379 Members | 1,191 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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

//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
0 1000

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

Similar topics

2
by: TJ | last post by:
Dear Sir/Madam I have made one user control to embed in Internet Explorer I just wrote one method to keep track log something like. public static void WriteLog(..) StreamWriter stWriter =...
1
by: Nguyen Thanh Danh | last post by:
I'm a student, i try search on Internet but i can't find any sample how to send file throught socket, between 2 computer! Please help me! Thanks!
1
by: michelevirgilio | last post by:
Hi, some hours ago my .net framework has stopped working, WITHOUT ANY REASON: no new installations, etc. Now every aspx page give me the error: Description: An error occurred during the...
3
by: broli85 | last post by:
I am writing an extension for Firefox 1.5. I would like to know if there is a way to write and read data to and from a file which is in the location of the extension (or somewhere in the user's...
7
by: nonameisfree | last post by:
I am trying to get StartUp-Manager to write the grub password in encrypted form to menu.lst. Currently it writes the password in plain-text and that is not too clever... This is the function I...
2
by: =?Utf-8?B?RGFtZW9u?= | last post by:
Hi - I am attempting to write lines to a file at high volume, multiple threads. Here is my scenario: (initial "WriteToFile" object created via a parent multithreaded process, which receives...
16
by: Hans Fredrik Nordhaug | last post by:
I'm trying to write to a file in the current directory - no remote files. The subject says it all - I can add that both the directory and the file is wordwritable. This happens on a (quite good)...
0
by: ali mostafa | last post by:
hi evry body, i want to send file throgh anetwork, i need fast help ----------please
1
by: pauld | last post by:
Hello - I have created a simple web application in VS 2005/ASP.net 2.0 for a university department to track problems with a new website that I have built for them. The app consists of an Access...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.