iphone - How to Transfer Large Files over wifi in iOS -
i downloaded witap code apple's website. transferring data on local wifi network. working in project interact client - server architecture. sending nsdata client side server.
i made 2 projects; 1 client , 1 server
at client side project, made following changes modified appcontroller.m file adding following method
appcontroller.m (client side)
- (void)senddata:(nsdata*)pobjdata { assert(self.streamopencount == 2); if ( [self.outputstream hasspaceavailable] ) { nsinteger byteswritten; nsuinteger length = [pobjdata length]; byteswritten = [self.outputstream write:[pobjdata bytes] maxlength:[pobjdata length]]; nslog(@"written bytes -> %d",byteswritten); } }
then calling method send data.
at server side project, made following chagnes modified appcontroller.m file modifying following method
appcontroller.m (server side)
- (void)stream:(nsstream *)stream handleevent:(nsstreamevent)eventcode { #pragma unused(stream) switch(eventcode) { case nsstreameventopencompleted: { self.streamopencount += 1; assert(self.streamopencount <= 2); // once both streams open hide picker , game on. if (self.streamopencount == 2) { [self dismisspicker]; [self.server deregister]; } } break; case nsstreameventhasspaceavailable: { assert(stream == self.outputstream); // nothing } break; case nsstreameventhasbytesavailable: { if (stream == self.inputstream) { nsinteger bytesread; uint32_t buffer[32768]; nsmutabledata *_data = [nsmutabledata data]; // pull data off network. bytesread = [self.inputstream read:buffer maxlength:sizeof(buffer)]; if (bytesread == -1) { } else if (bytesread == 0) { } else { // fixme: popup alert const long long expectedcontentlength = bytesread; nsuinteger expectedsize = 0; // expectedcontentlength can represented nsuinteger, cast it: expectedsize = (nsuinteger)expectedcontentlength; [_data appendbytes:buffer length:expectedsize]; nslog(@"\"data received has length: %d", _data.length); [self performselector:@selector(getdata:) withobject:_data afterdelay:1.0]; } } } break; default: assert(no); // fall through case nsstreameventerroroccurred: // fall through case nsstreameventendencountered: { [self setupfornewgame]; } break; } }
and added method write received data file
#define kuserdirectorypath nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) -(void)getdata:(nsmutabledata *)pdata { nsfilemanager *tmpmanager = [nsfilemanager defaultmanager]; [tmpmanager createfileatpath:[appcontroller getdocumentdirectorypath:[nsstring stringwithformat:@"%@.png",[nsdate date]]] contents:pdata attributes:nil]; } +(nsstring*)getdocumentdirectorypath:(nsstring*)pstrpathname { nsstring *strpath=nil; if(pstrpathname) strpath = [[kuserdirectorypath objectatindex:0] stringbyappendingpathcomponent:pstrpathname]; return strpath; }
i convert .png files nsdata , send them client side server side. server downloads file document directory
the matter , when transfer file client side , gets downloaded server side @ document directory. works fine in case of tiny files. if file size exceeds 8kb , file written @ document directory gets corrupted.
kindly me able send large files.
the problem code doesn't loop gather of available data till end (or loop send data either). ever receive first buffer of data. if image small works ok, if image bigger never will.
you need write code keeps sending when there buffer space until data sent , keep reading data (into nsmutabledata
instance variable, not local variable) until end of stream reached.
Comments
Post a Comment