IOS USES CocoaAsyncSocket8203;

  • 2020-05-15 02:14:06
  • OfStack

Socket

Now in the application of Socket iOS more slowly, is an instant messaging, many applications are integrated, the instant messaging function 1, it must be based on Socket, Socket this topic I 1 straight talk less, 1 is due to have seen some C/C + + operation scare Socket code 1 straight and psychological shadow, 2 is no how to meet the demand of the related application in the work, so also did not go to study related content.

Haven't contacted Socket programming will certainly heard of this a few key words: package, shaking hands, connections, TCP, UDP, etc., Socket programming articles online a lot, and as a beginner's mind is always on the Vincent sonic, before I, too, so the least, hope after reading this article you will be in iOS Socket programming on the platform has a simple understanding, that is don't be afraid!

Socket on iOS

On iOS, no, in the Cocoa world, Socket should be programmed in the following ways:

sys/ socket. h - the underlying Socket programming interface
Encapsulation of socket in CFNetwork - Core Foundation
Encapsulation of CFNetwork in NSStream-Cocoa
Of course, these are all big brother level, but we don't want to write C, of course, we can use a higher abstract open source library, such as:

CocoaAsyncSocket
SocketRocket
Socket.IO
CocoaAsyncSocket

I didn't have the opportunity to write Socket on iOS platform before. Recently, I encountered a function that required Socket to upload and download files. So we all know that Socket programming on iOS is CocoaAsyncSocket, and we didn't find any other better substitute.

CocoaAsyncSocket is a pure Objective-C open source library that is very simple to use and of course compatible with Swift's syntax.

TCP & UDP

If you see the description on the CocoaAsyncSocket home page, you will notice that there are two interfaces, TCP and UDP. To put it simply, Socket supports two protocols, one is TCP, the other is UDP. TCP is connection-oriented, requiring several "handshakes" per request, and the data is orderly and reliable. UDP is simpler. It doesn't shake hands, it sends packets out of order, it doesn't have to stay connected, it doesn't have to do any other open transmission overhead, and it consumes less bandwidth. Then, two sets of API are packaged for TCP and UDP respectively, which are named TCP or UDP.

GCD & Run-loop

A close look at CocoaAsyncSocket's naming of its classes reveals that apart from TCP and UDP, API is divided into two sets: GCDAsyncSocket/GCDAsyncUdpSocket based on GCD program security, AsyncSocket/AsyncUdpSocket based on Run-loop, and AsyncSocket/AsyncUdpSocket based on Run-loop. Generally speaking, we use GCD based encapsulation.

In practice, I think most of the requirements are based on TCP, so let's focus on GCDAsyncSocket. When you see GCD, don't assume that all of its callbacks are GCD.

GCDAsyncSocket

1 kind iOS socket do client mostly in the end, so use 1 some basic operation is connect, read, write, disconnect, GCDAsyncSocket encapsulation is very simple, started at the beginning of possible problems is a complete socket operation process is not clear, as long as the touch clear server-side logic process is very easy, such as need how many bytes to read first, and then parse out some data from the results, how many bytes, then read reanalysis data, read and write...

conclusion

In fact, Socket is not as complex as imagined. Many low-level work frameworks have been done for us, so we can focus more on business logic.