IPhone socket Development

Source: Internet
Author: User

Q: given that + Getstreamstohost: Port: inputstream: outputstream: Is not supported on iPhone OS, how can I create nsstreams for a TCP connection to a named host?

A: You can do this by exploiting the toll-free bridge between nsstream and cfstream. UseCfstreamcreatepairwithsockettohost To create cfstreams to the host, and then cast the resulting cfstreams to nsstreams.

Listing 1 Shows an example of this. It adds a category to nsstream that implements a replacement+ Getstreamstohost: Port: inputstream: outputstream:.

Listing 1: A category to creates TCP streams to a named host

@ Interface nsstream (myadditions) + (void) getstreamstohostnamed :( nsstring *) hostname port :( nsinteger) Port inputstream :( nsinputstream **) inputstreamptr outputstream :( nsoutputstream **) outputstreamptr; @ end @ implementation nsstream (myadditions) + (void) getstreamstohostnamed :( nsstring *) hostname port :( nsinteger) Port inputstream :( nsinputstream **) inputstreamptr outputstream :( nsoutputstream **) outputstream PTR {cfreadstreamref readstream; cfwritestreamref writestream; Assert (hostname! = Nil); Assert (Port> 0) & (Port <65536); Assert (inputstreamptr! = NULL) | (outputstreamptr! = NULL); readstream = NULL; writestream = NULL; cfstreamcreatepairwithsockettohost (null, (cfstringref) hostname, port, (inputstreamptr! = Nil )? & Readstream: NULL), (outputstreamptr! = Nil )? & Writestream: NULL); If (inputstreamptr! = NULL) {* inputstreamptr = [nsmakecollectable (readstream) autorelease];} If (outputstreamptr! = NULL) {* outputstreamptr = [nsmakecollectable (writestream) autorelease] ;}}@ end

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.