Analysis of Network Programming for iOS development (2)

Source: Internet
Author: User

Cfstream

Read/write streams provide a simple way to exchange data in the form of device dependencies. You can create a stream for the data in the memory, the data in the file, or the data on the Network (using sockets), instead of reading all the data into the memory at one time. Socket stream provides an easy-to-use interface for reading and writing data through sockets. Each socket can be bound to an input stream and an output stream to allow asynchronous communication. Cfstream encapsulates most of the work required for read and write throttling, replacing the traditional send and Recv methods in C language. Cfsream objects include cfreadstream and cfwritestream.

1. cfreadstream

It provides a series of methods for reading data from the socket. Some common functions are listed below.

(1) cfreadstreamopen and cfreadstreamclose open and close a read stream to allocate or release related resources. You can only read data from the read stream after it is opened.

(2) The cfreadstreamread function reads data from the stream and returns the actual number of bytes read. This method will be blocked until data is received.

(3) cfreadstreamgetbufer returns a pointer to the unread data cache in the read stream.

(4) cfreadstreamgetstatus returns the current status of the read stream.

 

Read the current status of the stream
  • Kcfstreamstatusnotopen (the read stream is not opened)
  • Kcfstreamstatusopening (the read stream is being opened)
  • Kcfstreamstatusopen (the read stream has been opened)
  • Kcfstreamreading (Reading In streaming)
  • Kcfsreamstatusatend (no data can be read from the Stream)
  • Kcfstreamstatusclosed (the read stream has been disabled)
  • Kcfstreamstatuserror (An error occurred while reading the Stream)

(5) cfreadstreamhasbytesavailable.

 

Returns a Boolean value indicating whether data can be read without blocking. If you cannot determine whether data exists without using the actual read operation function, true is returned.

(6) cfreadstreamshedulewithrunloop and cfreadstreamunschedulefromrunloop.

Call cfreadstreamshedulewithrunloop to specify the run loop and run loop modes. The callback function set using the cfreadstreamsetclient method is called when an event occurs. For example, a callback function is called when a stream is opened or data can be read. Use the cfreadstreamunschedulefromrunloop function to remove data from the run loop.

(7) cfreadstreamsetclient.

Set the callback function to be called when some events occur. The callback function declaration is similar to the following.

Void callbacktest (

Cfreadstreamref stream,

Cfstreameventtype eventtype,

Void * clientcallbackinfo

)

Similar to the cfsocket callback function, the callback function can obtain the event type and related data. The event type is defined as follows.

Typedef Enum

{

Kcfstreameventnone = 0 (no event occurred)

Kcfstreameventopencompleted = 1 (the stream is successfully opened)

Kcfstreameventhasbytesavailable = 2 (data can be read)

Kcfstreameventcanacceptbytes = 4 (the stream can accept the written data (used to write the stream ))

Kcfstreameventerroroccurred = 8 (an error occurred on the Stream)

Kcfstreameventendencountered = 16 (reached the end position of the Stream)

} Cfstreameventtype;

 

2. cfwritestream

Cfwritestream corresponds to cfreadstream. It provides a method for writing data (sending data) to the socket stream. Some common functions are listed below.

(1) cfwritestreamopen and cfwritestreamclose

Open and Close a write stream to allocate or release related resources. Data can only be written to a stream after it is opened.

(2) cfwritestreamwrite

The function that writes data to the stream and returns the actual number of bytes sent. This method will be blocked until the data is sent.

(3) cfwritestreamstatus

Returns the current status of the write stream.

  • Kcfstreamstatusnotopen (the write stream is not opened)
  • Kcfstreamstatusopening (the write stream is being opened)
  • Kcfstreamstatusopen (the write stream has been opened)
  • Kcfstreamwriting (write operation in streaming)
  • Kcfstreamstatusatend (no data can be written to the Stream)
  • Kcfstreamstatusclosed (the write stream has been closed)
  • Kcfstreamstatuserror (An error occurred while writing the Stream)

(4) cfwritestreamcanacceptbytes

Returns a Boolean value indicating whether data can be written to the stream without blocking. If you cannot determine whether to write data without using the actual write operation function, true is returned.

(5) cfwritestreamshedulewithrunloop and cfwritestreamunschedulefromrunloop

Call cfwritestreamshedulewithrunloop to specify the run loop and run loop modes. The callback function set using the cfwritestreamsetclient method is called when an event occurs. For example, the callback function is called when a stream is opened and data events can be sent. Use the cfwritestreamunschedulefromrunloop function to remove data from the run loop.

(6) cfwritestreamsetclient

Set the callback function to be called when some events occur. The callback function declaration is similar to the following.

Void callbacktest (

Cfwritestreamref stream,

Cfstreameventtype eventtype,

Void * clientcallbackinfo

);

Similar to the cfsocket callback function, the callback function can obtain the event type and related data.

 

 

 

Related Article

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.