In fact, in many applications development of iOS, most of the network communication is the HTTP/HTTPS protocol, unless there are special requirements to use the Socket network protocol for network data transmission, At this time, the iOS client needs a good third-party cocoaasyncsocket to connect and transfer data, the third-party address: Https://github.com/robbiehanson/CocoaAsyncSocket, Readers can google or Baidu to search the use of this library, there is a lot of information on the Internet, and usage is not difficult.
In some applications where the use of socket communication is not very high, such as the need to chat with multiple iOS devices for instant messaging, so long as the use of this cocoaasyncsocket can basically meet this demand, However, I am engaged in the live project on the socket communication protocol requirements are relatively high, this demand pattern and game development model is similar, because there are many game application data changes are needed to update the data in real-time, such as the game's player health, if in the multiplayer network game platform, Your device needs to update the health of other players in real-time data, and other players on the device will also update your health data in real-time, this demand through HTTP unsolicited request is not enough to meet the demand, so game development will generally have socket communication. In the background of the live project I am engaged in the language is the Erlang language, the background of the data transmitted by the socket has a custom protocol rules, such as:
Figure 1-1
This protocol is a binary stream that can be transmitted in the socket, the third part of the protocol data flow is the specific data field to be transferred,
And this Protocol data flow content is the following example of a protocol document through Erlang:
========== switch to a new scene ==========
Message cs_20001{
UInt32 Sceneid = 1; Enter Scene ID
UInt32 line = 2; Split line, public scene hair 0, multi-person replica backend will specify
}
Message sc_20001{
Uint8 Res = 1; 1 Success 0 Exception 2 cannot enter the level 3 is already in the level when it is 4 no this map or level 5 today into the boss or the normal number of times the upper limit 6 physical less than 7 entry conditions less than 8 map type does not exist 10 did not participate in the boss activity, unable to enter one boss battle room status has ended 12 times Less than 13 cooldown less than 14 no permission to enter this boss Room 15 into boss room condition insufficient
UInt32 Sceneid = 2; Now the scene
Uint8 Pos = 3; Sitting punctuation
Uint8 Status = 4; Character status 0 Normal 10 captain status
Uint8 Type = 5; Front End identification
UInt32 Defaultcombat = 6; Recommended Combat Effectiveness
}
First explain some of the definitions of this protocol:
CS is client-to-server (the data protocol that the client sends to the server), and SC is the Server-->client (the data protocol that the server returns to the client)
This article mainly explains the request module, so the CS_20001 request protocol encapsulates the packet, this 20001 is the number of this Protocol, also known as the Protocol ID (later in the code will be used).
In cs_20001, there are two fields that require the socket to be sent to the server, both in UInt32 format and in the basic data type of the C language, but in the socket transmission is a binary stream, that is, We need to convert these two UInt32 format data fields into binary data, then stitch it into a data stream, and then let the data stream pass to the server in the socket, when it comes to streaming. If you learn Java, the concept of convection is easier to understand, if you do not have contact with the flow does not matter, It's like a piece of water flowing from the client to the server. In fact, this data flow from the client to the server, the process is not as simple as imagined, involving a lot of the underlying socket transport logic logic, but Cocoaasyncsocket has done this part of the package, and is OC object-oriented encapsulation, We just need to transfer the data needed to nsdata through the Cocoaasyncsocket proxy method to pass the past.
For simple needs, such as I only need two iphone devices to pass the NSString string, as long as the nsstring to NSData transfer, but for I said that the Erlang server needs to customize the protocol, the client needs more packet unpacking logic, This packet, such as the above field protocol as an example, is the CS_20001 packet according to the figure 1-1 in the custom format for splicing data, then in figure 1-1 need two bytes of protocol number is 20001, That is, you need to store 20001 in two bytes of storage space, and then in figure 1-1 , the content of the protocol data flow is UInt32 Sceneid and UInt32 line the two fields splicing data, and the protocol specifies the order of concatenation is what order, Here UInt32 Sceneid of course is in front and UInt32 line after stitching well, can calculate the number of bytes of this protocol data stream? Bytes, then +2 (the byte length of the protocol number), and +4 (the four bytes required for the total length of the message), the length of the entire protocol stream is obtained, The total length is then stored in four bytes of the total message length, of course, the entire protocol flow stitching sequence is shown in figure 1-1 , and then passed through Cocoaasyncsocket to the server.
The following is a code to explain this business logic:
First, the background of the Protocol provided by the model object, whenever there is an MVC basis should be second understand, in fact, is the model of MVC.
iOS Development Socket Communication Combat--request Request module