Upload File Block Client implementation

Source: Internet
Author: User


First, the content blocks all files (block size constraints), and then for each chunk constructs a separate UDP datagram for transmission, at the beginning of the application layer is its own definition of the header, there is block number, block length, block fingerprint and other metadata information, which facilitates the receiving end can be correctly received in order.


/*--vonzhou---This project was to upload the file after chunking using Rabin fingerprint, where use UDP so that every Packe    T Nodelay. But we need to ensure the reliability.*/#include "global.h" #define Serv_port 2500typedef struct {unsigned char fp[        20];        int chunk_id;        Short flags; Short Chunk_len; The Max is 32767 enough char data[max_chunk_size];}        transferunit;//Some flags for this chunk transfered.enum{chunk_need_dedu = 0x0001,//Need deduplication Chunk_other = 0x0002//other for extension}; FileInfo *fi;int sendudp (FileInfo *fi,int sockfd,struct sockaddr *pservaddr,socklen_t servlen) {char buf[sizeof (TRANSFE    Runit)];    Fd_set Wrset;    struct Timeval TV;    int Rlen,wlen, len = 1;    int FD;    int ret;    int i,j;    Fingerchunk *p;    Transferunit Unit;    Connect to server if (connect (SOCKFD, struct sockaddr *) pservaddr,servlen) = =-1) err_quit ("Connet error"); else printf ("Connect Server ok!\n");    FD = open (Fi->file_path, o_rdonly);    if (fd==-1) err_quit ("fopen error%s\n", Strerror (errno));                Begin to transfer the file to the server;    i=0;    p = fi->first;        while (1) {tv.tv_sec = 1;//, select to wait 1 second;        tv.tv_usec = 0;        Fd_zero (&wrset);        Fd_set (Sockfd,&wrset);        Wait for the socket can write RET = select (SOCKFD+1,NULL,&WRSET,NULL,&TV);        if (ret = =-1) err_quit ("Select Error%s\n", Strerror (errno));            else if (ret==0) {printf ("Select Timeout,continue circle\n");        Continue        }//prepare for writing the socket memset (&unit, 0, sizeof (unit));             if (Fd_isset (Sockfd,&wrset)) {/* We should first transfer some medadata,like filename,filter feature vector, *fixme:file fingerprint to controller?

To make a packetin, *here just let the FP = 1s */if (i = = 0) {for (j=0; j<20;j++) Unit.fp[j] = 0xFF; unit.chunk_id = 0; Need not dedu Unit.chunk_len = strlen (Fi->file_path); Wlen = Write (SOCKFD, &unit, + strlen (Fi->file_path)); if (Wlen! = (strlen (fi->file_path))) Err_quit ("Write data to SOCKFD error:%s\n", Strerror (errno) ); memset (&unit, 0, sizeof (unit)); if (P! = NULL) len = p->chunklen; The last read, p was null, so cannnot use P->chu Rlen = Read (FD, Unit.data, Len); if (Rlen < 0) err_quit ("Fread Data error%s\n", Strerror (errno)); else if (rlen==0) {//indicate The transfer completed for (j=0;j<20;j++) UNIT.FP[J] = 0xFF; unit.chunk_id = 0; Need not dedu Unit.chunk_len = 3; strncpy (Unit.data, "End", 3); Wlen = Write (sockfd,&unit,28 + 3); if (Wlen!=31) err_quit ("Write End Flag error:%s\n", Strerror (errno)); printf ("File%s Transfer success!\n", Fi->file_path); Close (FD); return 0; }//construct this tranfer unit we cannot before read//bcos the following p pointer used. for (j=0; j< 20;j++) unit.fp[j] = p->chunk_hash[j];//20B fingerprint; unit.chunk_id = i; 4 b chunk ID Unit.chunk_len = p->chunklen; Unit.flags = chunk_need_dedu; Write to Socket Wlen = Write (SOCKFD, &unit, 28+p->chunklen); if (Wlen! = (Rlen +)) err_quit ("Write data to SOCKFD error:%s\n", Strerror (errno)); i++; p = p->next; memset (&unit, 0, sizeof (unit)); Usleep (500); printf ("The%d Times read\n", I); }}//End while (1)}int main (int argc, char *argv[]) {char *fh; struct SysInfo s_info; Long time1,time2; int Error1,error2; int sockfd; struct stat fsize; struct sockaddr_in servaddr; error1= SysInfo (&s_info); time1 = S_info.uptime; int R; FileInfo *fi; fi = File_new (); if (argc! = 3) err_quit ("useage:udpclient<ipaddress>;\n"); Bzero (&servaddr,sizeof (SERVADDR)); Servaddr.sin_family= af_inet; Servaddr.sin_port = htons (Serv_port); if (Inet_pton (AF_INET,ARGV[1],&AMP;SERVADDR.SIN_ADDR) <= 0) err_quit ("[%s]is not a valid ipaddress\n", argv[1] ); SOCKFD =socket (af_inet,sock_dgram,0); R = Fcntl (SOCKFD, F_GETFL, 0); Fcntl (SOCKFD, F_SETFL, R & ~o_nonblock); ChunkinG File strcpy (Fi->file_path, argv[2]); Chunk_file (FI); printf ("File Size:%lld\n", fi->file_size); printf ("Chunk Num:%d\n", fi->chunknum); Sendudp (FI, SOCKFD, (struct sockaddr *) &servaddr,sizeof (SERVADDR)); Close (SOCKFD); fprintf (stderr, "serverip:\t%s\n", argv[1]); if (stat (argv[2],&fsize) = =-1) perror ("Failed to get Fiel statusi\n"); else fprintf (stderr, "File Name:\t%s\nfile size:\t%dk\n", argv[2],fsize.st_size/1024); Error2=sysinfo (&s_info); Time2 = S_info.uptime; printf ("Tranfice file time =%ld seconds\n", (time2-time1));}


Reprint Please specify source: http://blog.csdn.net/vonzhoufz/article/details/31429585

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Upload File Block Client implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.