- (void) Startsocket: (NSString *) address Andport: (int) port{Cfreadstreamref readref; Cfwritestreamref Writeref; Cfstreamcreatepairwithsockettohost (NULL, (__bridge cfstringref) address, port,&readref, &writeref); _inputstream= (__bridge Nsinputstream *) Readref; _outputstream= (__bridge Nsoutputstream *) Writeref; //Set flow Properties[_inputstream Setproperty:nsstreamnetworkservicetypevoip Forkey:nsstreamnetworkservicetype]; [_outputstream Setproperty:nsstreamnetworkservicetypevoip Forkey:nsstreamnetworkservicetype]; //Set up proxy_inputstream.Delegate=Self ; _outputstream.Delegate=Self ; //put it in the Runloop.[_inputstream scheduleinrunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode]; [_outputstream Scheduleinrunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode]; [_inputstream Open]; [_outputstream open];}//disconnecting Scoket Connections- (void) closesocket {if(_inputstream) {_inputstream.Delegate=Nil; [_inputstream Close]; [_inputstream Removefromrunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode]; _inputstream=Nil; } if(_outputstream) {_outputstream.Delegate=Nil; [_outputstream Close]; [_outputstream Removefromrunloop:[nsrunloop Mainrunloop] formode:nsdefaultrunloopmode]; _outputstream=Nil; [Svprogresshud showsuccesswithstatus:@"Disconnected successfully"]; } //notification to the interface[[Nsnotificationcenter Defaultcenter] Postnotificationname:@"closesocket" Object: nil Userinfo:nil];}//The proxy callback is in the main thread#pragmaMark-<NSStreamDelegate> Proxy Method-(void) Stream: (Nsstream *) Astream handleevent: (nsstreamevent) eventcode{Switch(EventCode) { CaseNsstreameventnone: Break; Casensstreameventopencompleted: [Svprogresshud showsuccesswithstatus:@"Connection Successful"]; NSLog (@"successfully established the connection to form the input/output pipeline"); Break; CaseNsstreameventhasbytesavailable:nslog (@"have data to read"); {uint8_t buf[3072]; Nsinteger Len=0; Nsinputstream*inputstream = (Nsinputstream *) Astream; Len= [InputStream read:buf maxLength:3072]; if(len) {_tempstring= [nsmutablestringstring]; //Stitching 16 characters together for(inti =0; i < Len; i++) {nsmutablestring*STR = [Nsmutablestring stringWithFormat:@"%x", Buf[i]]; if(Str.length <2) {[str insertstring:@"0"Atindex:0]; } [_tempstring Appendstring:str]; } //Sub-package, sticky packet processing, simultaneous KVO monitoring of available packages[_stick_subcontract_package handleresultonreceivedata:_tempstring]; } Break; } CaseNsstreameventhasspaceavailable:nslog (@"can send data"); Break; CaseNsstreameventerroroccurred:nslog (@"An error occurred and the connection failed"); [Svprogresshud showerrorwithstatus:@"Connection Error"]; {[Astream close]; Break; } Casensstreameventendencountered: [Svprogresshud showerrorwithstatus:@"Normal Disconnect"]; NSLog (@"Normal Disconnect"); {[Astream close]; [Astream Removefromrunloop:[nsrunloop Currentrunloop] formode:nsdefaultrunloopmode]; Astream=Nil; Break; } default: Break; }}
One problem is that this does not implement asynchronous accept messages. So it's best to use Asynsocket.
IOS-native Nsstream implementation socket