IOS-native Nsstream implementation socket

Source: Internet
Author: User

- (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

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.