Upload
chriskee
View
221
Download
0
Embed Size (px)
Citation preview
7/31/2019 Read Me About SimpleNetworkStreams
1/2
ReadMeAboutSimpleNetworkStreams==================================1.2
SimpleNetworkStreamsshowshowtodosimplenetworkingusingtheNSStreamAPI.Thegoalofthissampleisverylimited:itdoesnotdemonstrateeverythingyouneedtoimplementafullyfledgednetworkingproduct(moreonthisbelow),rather,itfocusesonusingtheNSStreamAPItomovearealisticamountofdataacrossthenetwork.
SimpleNetworkStreamsrequiresiOS4.0orlater,althoughthecorenetworkingcodeshouldalsoworkoniPhoneOS2.xandMacOSX.
PackingList------------Thesamplecontainsthefollowingitems:
oReadMeAboutSimpleNetworkStreams.txt--Thisfile.oSimplifyingAssumptions.txt--Moredocumentation.oSimpleNetworkStreams.xcodeproj--AnXcodeprojectforthesample.oResources--Theprojectnib,images,andsoon.oAncillaryCode--Adirectoryfullofcodethat'snotdirectlyrelevanttothemainfunctionofthissample.oSendController.[hm]--Aviewcontrollerthatsendsfiles.
oReceiveController.[hm]--Aviewcontrollerthatreceivesfiles.oReceiveServerController.[hm]--Aviewcontrollerthatimplementsaservertoreceivefiles.oSendServerController.[hm]--Aviewcontrollerthatimplementsaservertosendfiles.
UsingtheSample----------------Youcantestthesamplewithonedevice(usingloopback),onesimulator(usingloopback),twodevices,twosimulators(ondifferentmachines),adeviceandasimulator,adeviceandtheMaccommandline,andasimulatorandtheMaccommandline.AlldevicesandsimulatorsmustbeonthesameWi-Finetwork.
TestingwiththeGUIissimple:
1.Runtheprogramononedevice(orsimulator).
2.Switchtothe"ReceiveServer"tabandtapStart.
IMPORTANT:Iftheserverfailstostartwiththemessage"Registrationfailed",it'slikelythatanothercopyoftheserverisrunningonthesamenetwork.Formoreinformationaboutthislimitation,see"SimplifyingAssumptions"below.
3.Switchtothe"SendServer"tabandtapStart.
4.Ifyou'reusinganotherdevice(orsimulator),runtheprogramonthat.
5.Switchtothe"Send"tabandtaponeoftheimagestosend.
6.Switchtothe"Receive"tabandtapReceivetoreceiveanimage.
IfyouwanttotestagainsttheMaccommandline,switchtotheInfotabforinstructions.
Theprogramhasfourbuilt-intestimageswithexponentiallyincreasingsize.Transferringthefirstimage,shownatthetopleft,willbeveryquick.Transfe
7/31/2019 Read Me About SimpleNetworkStreams
2/2
rringthelastimage,shownatthebottomright,willbeveryslow(athousandtimesslower!).Thisallowsyoutotestvariousthingsthatyoucouldn'ttestotherwise,liketheStopandCancelbuttons.See-[AppDelegatepathForTestImage:]formoreinformationonhowtheselargeimagesarecreated.
BuildingtheSample-------------------ThesamplewasbuiltusingXcode3.2onMacOSX10.6withiPhoneSDK4.0.YoushouldbeabletojustopentheprojectandchooseBuildfromtheBuildmenu.TheresultingprogramshouldbecompatiblewithalldevicesrunningiOS4.0andlater,althoughthebulkofmytestingwasdonewithaniPhonetouch(secondgeneration)runningiOS4.0.
HowitWorks------------ThesampleisaverysimpleapplicationoftheNSStreamAPIfornetworking.Eachviewcontrollerisaself-containednetworkingexample.Theclientviewcontrollers(SendControllerandReceiveController)createanNSNetServiceforahard-codedBonjourservicename,thencreateanNSOutputStreamorNSInputStreamfromthatnetservice,thenschedulethatstreamforasynchronousoperations.Thebulkoftheinterestingcodeisinthe-_startSend/_startReceiveand-stream:handleEvent:methods.
Theserverviewcontrollers(ReceiveServerControllerandSendServerController)a
realittlemorecomplex.TheycreatealisteningTCPsocket,wrapthatsocketinaCFSocket,andthenregisteritwithBonjourusingNSNetService.Whenaconnectionarrives,theywraptheconnection'ssocketinanNSStreamandthenschedulethatstreamforasynchronousoperations.Thebulkoftheinterestingcodeisinthe-_startServer,_startSend/_startReceive,and-stream:handleEvent:methods.
SimplifyingAssumptions-----------------------TomakethissampleeasytounderstandImadeanumberofsimplifyingassumptions.Ifyou'recreatingarealapplication,youshouldreadthe"SimplifyingAssumptions.txt"documenttoseewhichoftheseassumptionsmightaffectyourapplication,andforpointersonhowtoturnthissamplecodeintoproduction-quality
code.
CreditsandVersionHistory---------------------------Ifyoufindanyproblemswiththissample,pleasefileabugagainstit.
1.0(May2009)wasthefirstshippingversion.
1.1(Sep2009)implementedsomecosmeticchangestobringitinlinewithother,similarsamples.
1.2(June2010)updatedforiOS4.0.
ShareandEnjoy
AppleDeveloperTechnicalSupportCoreOS/Hardware
11Sep2009