This article and related series is my summary of the iOS network development knowledge Point, this article is the second one, mainly analyzes several important classes in cocoa streams
Cocoa streams is actually a simple package of objective-c for Cfnetwork, consisting mainly of three classes: Nsstream, Nsinputstream, and Nsoutputstream. The interface of this section is simple and the usage is very clear. I'll just list the interfaces here for easy access. I don't know about cfnnework. Network Programming for iOS Research (i)-cfnetwork use detailed
The Nsstream interface is as follows:
@interface Nsstream:nsobject-(void) open; -(void) close; -(ID <NSStreamDelegate>) delegate; -(void) Setdelegate: (id <NSStreamDelegate>) delegate; By default, a stream is it own delegate, and subclassers of Nsinputstream and Nsoutputstream must maintain this contra Ct. [Somestream Setdelegate:nil] must restore this behavior. As usual, delegates is not retained. -(ID) Propertyforkey: (NSString *) key; -(BOOL) SetProperty: (ID) Property Forkey: (NSString *) key; -(void) Scheduleinrunloop: (Nsrunloop *) Arunloop Formode: (NSString *) mode; -(void) Removefromrunloop: (Nsrunloop *) Arunloop Formode: (NSString *) mode; -(Nsstreamstatus) streamstatus; -(Nserror *) Streamerror; @end @protocol nsstreamdelegate <NSObject> @optional-(void) stream: (Nsstream *) Astream handleevent: (Nsstreamev ENT) EventCode; @end #if (Target_os_mac &&! target_os_embedded | | Target_os_iphone)//cannot be used on iOS @interface Nsstream (nssocketstreamcreationextensions) + (void) Getstreamstohost: (Nshost *) host port: (Nsinteger) port InputStream: (Nsinputstream *) InputStream OutputStream: ( Nsoutputstream * *) OutputStream; @end #endif
12345678910111213141516171819202122232425262728 |
@InterfaceNsstream :NSObject - (void)Open; -(void)Close; -(ID<nsstreamdelegate>)Delegate; - (void)setdelegate:(ID<nsstreamdelegate>)Delegate; //By default, a stream is it own delegate, and subclassers of Nsinputstream and Nsoutputstream must maintain this CO Ntract. [Somestream Setdelegate:nil] must restore this behavior. As usual, delegates is not retained. -(ID)Propertyforkey:(NSString *)Key; - (BOOL)SetProperty:(ID) PropertyForkey:(NSString *)Key; - (void)Scheduleinrunloop:(Nsrunloop *)ArunloopFormode:(NSString *)Mode; -(void)Removefromrunloop:(Nsrunloop *)ArunloopFormode:(NSString *)Mode; -(Nsstreamstatus)Streamstatus; - (Nserror *)Streamerror; @End @Protocolnsstreamdelegate <NSObject> @Optional -(void)Stream:(Nsstream *)Astreamhandleevent:(nsstreamevent)EventCode; @End #if (Target_os_mac &&!) ( target_os_embedded | | Target_os_iphone)//cannot be used on iOS@InterfaceNsstream (nssocketstreamcreationextensions) + (void)Getstreamstohost:(Nshost *)HostPort:(Nsinteger)PortInputStream:(Nsinputstream **)InputStreamOutputStream:(Nsoutputstream **)OutputStream; @End #endif |
Nsinputstream and Nsoutputstream are inherited Nsstream, interfaces are as follows
<textarea class="crayon-plain print-no" style="line-height: 15px !important; overflow: hidden; font-size: 12px !important; z-index: 0; opacity: 0; -moz-tab-size: 4; -o-tab-size: 4; -webkit-tab-size: 4; tab-size: 4;" wrap="soft" readonly="" data-settings="dblclick">@interface Nsinputstream:nsstream-(Nsinteger) Read: (uint8_t *) buffer maxLength: (Nsuinteger) Len; -(BOOL) GetBuffer: (uint8_t * *) buffer length: (Nsuinteger *) Len; -(BOOL) hasbytesavailable; @end @interface Nsoutputstream:nsstream-(Nsinteger) write: (const uint8_t *) Buffer maxLength: (Nsuinteger) Len; -(BOOL) hasspaceavailable; @end</textarea>
12345678910 |
@InterfaceNsinputstream : Nsstream - (Nsinteger)Read:(uint8_t *)BufferMaxLength:(Nsuinteger)Len; -(BOOL)GetBuffer:(uint8_t **)Bufferlength:(Nsuinteger *)Len; - (BOOL)hasbytesavailable; @End @InterfaceNsoutputstream : Nsstream - (Nsinteger)Write:(Constuint8_t *)BufferMaxLength:(Nsuinteger)Len; -(BOOL)hasspaceavailable; @End |
But here the Getstreamstohost method is not available on iOS, so how do I initialize Nsstream on iOS? This is actually a small iOS bug, Apple has given the answer, we need to add a class for Nsstream:
Network Programming for iOS Research (ii)-COCOA streams use of detailed