The UDP File Sending function in xcode, which is used to send 8000 bytes per packet.

Source: Internet
Author: User

-(Void) udp_sendfile :( nsstring *) filename;

{

Nsstring * filepath = [nstemporarydirectory () stringbyappendingstring: Filename];

Nsdata * sdata = [[nsdata alloc] initwithcontentsoffile: filepath];

Byte * Sarr;

Int sumpak, curpak;

// Subpacket Transmission

Int Total = sdata. length, curloc, srctotal = sdata. length;

Sumpak = srctotal/8000;

If (srctotal % 8000 = 0)

{

}

Else

{

Sumpak = sumpak + 1;

}

Curpak = 0;

Curloc = 0;

Nslog (@ "Total: % d", total );

// File Information header: Ea filetype paknum1 paknum2 paknum2 paknum3 paksum1 paksum2 paksum3 paksum4 datalen1 datalen2 CHK1 CHK2 temp

While (total> 8000)

{

Curpak ++;

Nsdata * pdata = [sdata subdatawithrange: nsmakerange (curloc, 8000)];

Sarr = new byte [8016];

Byte * bytes;

Sarr [0] = 0xea; // header flag

Sarr [1] = 0xea; // header flag

Sarr [2] = 0; // file type

Sarr [3] = curpak/256/256/256; // Package number

Sarr [4] = curpak % (256*256*256)/256;

Sarr [5] = curpak % (256*256)/256;

Sarr [6] = curpak % (256*256) % 256;

Sarr [7] = sumpak/256/256/256; // Total number of packages

Sarr [8] = sumpak % (256*256*256)/256;

Sarr [9] = sumpak % (256*256)/256;

Sarr [10] = sumpak % (256*256) % 256;

Sarr [11] = 8000/256;

Sarr [12] = 8000% 256;

Int check = 0;

Bytes = (byte *) [pdata bytes];

For (INT I = 0; I <8000; I ++)

{

Check = check + bytes [I];

Sarr [I + 16] = bytes [I];

}

Sarr [13] = check/256;

Sarr [14] = check % 256;

Sarr [15] = 0;

Nsdata * sdata = [[nsdata alloc] initwithbytes: Sarr length: 8016];

[Udpclient senddata: sdata tohost: serverip port: 8009 withtimeout: 5000 Tag: 1];

Total = total-8000;

Curloc = fig + 8000;

Sarr = nil;

Nslog (@ "% d", 8000 );

}

If (total> 0)

{

Curpak ++;

Nsdata * pdata = [sdata subdatawithrange: nsmakerange (curloc, total)];

Sarr = new byte [Total + 16];

Byte * bytes = (byte *) [pdata bytes];

Sarr [0] = 0xea; // header flag

Sarr [1] = 0xea; // header flag

Sarr [2] = 0; // file type

Sarr [3] = curpak/256/256/256; // Package number

Sarr [4] = curpak % (256*256*256)/256;

Sarr [5] = curpak % (256*256)/256;

Sarr [6] = curpak % (256*256) % 256;

Sarr [7] = sumpak/256/256/256; // Total number of packages

Sarr [8] = sumpak % (256*256*256)/256;

Sarr [9] = sumpak % (256*256)/256;

Sarr [10] = sumpak % (256*256) % 256;

Sarr [11] = 8000/256;

Sarr [12] = 8000% 256;

Int check = 0;

For (INT I = 0; I <total; I ++)

{

Check = check + bytes [I];

Sarr [I + 16] = bytes [I];

}

Sarr [13] = check/256;

Sarr [14] = check % 256;

Sarr [15] = 0;

Nsdata * sdata = [[nsdata alloc] initwithbytes: Sarr length: Total + 16];

[Udpclient senddata: sdata tohost: serverip port: 8009 withtimeout: 5000 Tag: 1];

Nslog (@ "% d", total );

 

}

 

}

 

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.