iOS-based example Witap
1. Create a local service and set the listening time to detect if there is a device connection.
Nsnetservice * Server = [[Nsnetservice alloc] Initwithdomain:@"Local."Type:kwitapbonjourtype Name:@"Minwei"Port0];//[Uidevice currentdevice].nameif(Self.server! =Nil) {[Self.server setdelegate:self]; //self.server.includesPeerToPeer = YES; [Self.server publishwithoptions:nsnetservicelistenforconnections]; [Self.server scheduleinrunloop:[nsrunloop currentrunloop]formode:nsrunloopcommonmodes]; //[Self.server publish];
}
2. Create a local client search service.
Testservicebrowser = [[Nsnetservicebrowser alloc] init];[ Testservicebrowser setdelegate:self]; // testservicebrowser.includespeertopeer = YES; [Testservicebrowser searchforservicesoftype:kwitapbonjourtype Indomain:@ "local"];
3 When the service is found, the following methods are called to find and remove to determine how many IP connections are on the LAN.
- (void) Netservicebrowser: (Nsnetservicebrowser *) browser Didremoveservice: (Nsnetservice *) Service morecoming: (BOOL) morecoming{//[Netservice Removeobject:service]; if(!morecoming) {NSLog (@"Remove"); [NetServices Removeobject:service]; }}- (void) Netservicebrowser: (Nsnetservicebrowser *) browser Didfindservice: (Nsnetservice *) Service morecoming: (BOOL) morecoming{//Only update the UI once we get the no-more-coming indication. //[Netservice Addobject:service]; if(!morecoming) {NSLog (@"Find"); [Service setdelegate:self]; [NetServices Addobject:service]; //[Service resolvewithtimeout:5.0]; }}
4. Connect the user on the first pairing by opening Nsinputstream and Nsoutputstream
* * = [Service Getinputstream:&instream outputstream:&OutStream]; if ( ! success) { //re-connect else {self.inputstream == outstream; [Self openstreams];}
- (void) Stream: (Nsstream *) Stream handleevent: (nsstreamevent) eventcode{#pragmaUnused (stream)Switch(EventCode) { Casensstreameventopencompleted: {self.streamopencount+=1; ASSERT (Self.streamopencount<=2); //Once Both streams is open we hide the picker and the game is on. if(Self.streamopencount = =2) {//connection succeeded } } Break; Casensstreameventhasspaceavailable: {assert (Stream==Self.outputstream); // do nothing} Break; Casensstreameventhasbytesavailable: {
Callback information successful uint8_t B; Nsinteger Bytesread; ASSERT (Stream==Self.inputstream); Bytesread= [Self.inputstream read:&b maxLength:sizeof(uint8_t)]; if(Bytesread <=0) { //Do nothing ; we'll handle EOF and error in the//nsstreameventendencountered and nsstreameventerroroccurred case,//respectively.}Else { if(b!='#') {Passmessage[inum]=b; Inum++; } Else{printf ("%s", (Char*) passmessage); NSString*result = [NSString stringwithcstring: (Char*) Passmessage encoding:nsutf8stringencoding]; NSLog (@"%@", result); Inum=0; } return; } } Break; default: Assert (NO); //Fall through Casensstreameventerroroccurred://Fall through Casensstreameventendencountered: {//re-query links} Break; }}
Attached Openstreams, closestreams and send Message
- (void) openstreams{assert (Self.inputstream! = nil);//streams must exist but aren ' t openASSERT (Self.outputstream! =nil); ASSERT (Self.streamopencount==0); [Self.inputstream setdelegate:self]; [Self.inputstream Scheduleinrunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode]; [Self.inputstream Open]; [Self.outputstream setdelegate:self]; [Self.outputstream Scheduleinrunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode]; [Self.outputstream open];}- (void) closestreams{assert (Self.inputstream! = Nil) = = (Self.outputstream! = nil));//should either have both or neither if(Self.inputstream! =Nil) {[Self.inputstream removefromrunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; [Self.inputstream Close]; Self.inputstream=Nil; [Self.outputstream Removefromrunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode]; [Self.outputstream Close]; Self.outputstream=Nil; } Self.streamopencount=0;}
Send method
- (void) Send: (uint8_t) {NSLog (@"Send"); ASSERT (Self.streamopencount==2); //Only write to the stream if it has space available, otherwise we might block. //In the a real app you had to handle this case properly but in this sample code it ' s//OK to ignore it; if the stream stops transferring data the user was going to has//To tap a lot before we fill up our stream buffer (-: if([Self.outputstream hasspaceavailable]) {Nsinteger byteswritten; Nsuinteger datalength; Nsuinteger Sofar=0; Nsinteger written=0; NSData* data = [@"qwertitiww#"datausingencoding:nsutf8stringencoding]; Constuint8_t * m = (byte*) [data bytes]; Datalength=[data length]; Do{Byteswritten= [Self.outputstream Write:&m[sofar] Maxlength:datalength-Sofar]; ASSERT (Byteswritten!=0); if(Written ==-1){ Break; } Else{Sofar+=Byteswritten; } if(Byteswritten! =datalength) {[Self setupfornewgame]; Break; } } while(Sofar! =datalength); }}
Since the transfer can only accept uint8_t, it is necessary to transmit a bytes to transmit the string at the end of the # number is terminator to distinguish between the time to send a message
IOS LAN Send Information