Preparations for iOS IM Development (3) Talk about Socket and iossocket

Source: Internet
Author: User

Preparations for iOS IM Development (3) Talk about Socket and iossocket

If you are using IM, you need to use socket for communication. If you know about socket, you can skip this article to avoid being pitted by me.

As for what is socket, I will post two links, one is the blog /. The two links are good, but the layout of the blog garden looks more comfortable, and the C code of CSDN can be seen.

Generally, we make HTTP requests for apps: You send one, and the server returns one. Intuitively, from the perspective of data acquisition, the socket can passively obtain the information sent to you by the server (it may be inappropriate if no terminology is used, but what I want is a simple description ...), That is to say, you can also receive data information without a request. To achieve this, you need to maintain a persistent connection with the server, just like making a phone call.

Although it is not appropriate to use a phone call to give an example, we can refer to our behavior during the call. These behaviors are related in socket communication.

First, dialing is required for a call. This is a reason to establish a connection with the socket, so that someone at the end of the phone can answer the call. That is, when the server responds to you, it is considered connected.

Second, you need to know who the caller is. When there is no incoming call display, we have to ask who the recipient is. When there is a incoming call display, we will know the recipient's number. That is, we need to confirm who calls me. The same is true for socket. to do something, you must log on first.

Third, voice chat. You can speak and answer questions at the same time. Socket can be accepted and sent at the same time.

4. End the call. This may be due to many reasons, but you may hear the sound of hanging up. Or if the other party does not speak for a long time, you can call the phone by yourself. If you do not send messages for half a day, the server will be disconnected when you go offline. That is to say, a heartbeat packet is required.

Of course, the above is just a phone call. Socket still has some other attributes. Especially the information structure.

Socket communication packets are generally divided into two segments. A segment is called a header, and a segment is the body.

The header of the message, which generally contains the following information: the unique command CMD for the operation, the LENGTH of the body, and the authentication information of the message (generally customized ). The header length is generally fixed and may be unique and fixed. CMD is used to explain what I sent this time; LENGTH is used to explain what data I brought this time; authentication information is very important and related to security.

The message body is the useful information to be transmitted this time. Just follow CMD to unlock it.

When you see the above two paragraphs, You should know a few things:

1. After receiving the packet, you must unbind the packet header first. If there is an exception in this step, directly discard this header and read the next header.

2. The body length must be intercepted according to the header. The structure of the body to be unlocked must be handled by referring to CMD. If the length is 0, it must be processed. If CMD does not exist, it must be processed.

Someone will ask how to use iOS socket. I would like to say that the third party has helped you establish a connection to enable sending and receiving. What you need to do is to differentiate the header and body.

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.