Using Cocoaasyncsocket _ios in IOS

Source: Internet
Author: User
Tags file upload gcd

Socket

Now in IOS applications to the Socket slowly up, on an instant messaging, many applications have integration, that instant messaging function is generally certainly based on the socket, socket this topic I have been talking about less, one is because I have seen some C/s + + operation Socket Code I'm scared. There is still a psychological shadow exists, the second is not how to meet the needs of the application, so there is no in-depth research related content.

Have not touched the socket programming must also have heard these several keywords: package, handshake, Connection, TCP, UDP, and so on, Socket programming articles online a lot of, and as a beginner's mind always to these look Vincent timid, before I was so, so deep experience, hope that after reading this article you will be on the The Socket programming on the IOS platform has a simple understanding, that is not afraid!

Socket on IOS

On IOS, oh no, it should be said that Cocoa in the world, Socket programming should have the following several ways:

Sys/socket.h-System bottom Socket programming interface
Encapsulation of socket in Cfnetwork-core Foundation
Encapsulation of Cfnetwork in Nsstream-cocoa
Of course, above these are big Brother level, but just do not want to write C, we can use the abstraction of the higher open Source Library, for example:

Cocoaasyncsocket
Socketrocket
Socket.io
Cocoaasyncsocket

has not had the opportunity to write a socket on the iOS platform, recently encountered a need to use socket to do file upload download function, then IOS to do socket programming we all know is cocoaasyncsocket a single large, wood has found other better alternatives, so simple to see Look at the document, it will be on the battlefield.

Cocoaasyncsocket is a pure objective-c open Source Library that is simple to use and, of course, compatible with Swift's syntax.

TCP & UDP

If you see the Cocoaasyncsocket home page description should be noted that there are TCP and UDP two sets of interfaces, simple is the socket support two protocols, one is called TCP, a call udp;tcp-oriented connection, each request requires a few times "handshake", the data orderly and reliable; UDP Simple point, do not shake hands, the contract is also disorderly, do not have to maintain connection, also do not use some other open transmission overhead, bandwidth consumption is less. The Cocoaasyncsocket then encapsulates two sets of APIs for TCP and UDP, named respectively as TCP or UDP.

GCD & Run-loop

A careful look at Cocoaasyncsocket's naming of its classes reveals that, in addition to TCP and UDP splitting into two sets of APIs, a set of gcdasyncsocket/gcdasyncudpsocket based on GCD-Path security, a set of Run-loop-based Syncsocket/asyncudpsocket, generally we use the GCD based encapsulation.

In the practical application I think most of the requirements are based on TCP, so we mainly look at Gcdasyncsocket, see GCD do not think that all of its callbacks are GCD, read the source code you know all the callbacks are all Delegate.

Gcdasyncsocket

General IOS on the client side of the socket is mostly, so the use of some of the basic operation is connect, read, write, Disconnect,gcdasyncsocket encapsulation is very simple, the initial start may encounter problems lies in a complete Socket operation process is not clear, as long as the logic of the service side of the process is very easy, such as the need to read the number of bytes, and then from the results of a certain part of the data, and then read how many bytes, then analyze the data, read and write again ...

Summarize

In fact, the Socket is not as complex as imagined, many of the bottom of the work framework for us to do, can be more focused on the business logic.

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.