Socket frame continuation, asynchronous socket

Source: Internet
Author: User

The previous article on the socket is a simple encapsulation, but because send and recv are synchronous, the real use of the time generally need to asynchronous, if the application layer itself is responsible for maintaining the asynchronous thread, the framework is not practical, so asynchronous transceiver or to have.

Do not know how to say, first occupy the hole after the slowly say it.

My approach to implementation:

1, create Sendbuffer and Recvbuffer, package send and receive messages.

2. Using circular queues to create send and receive queues, an asynchronous call simply joins the message to the queue. (The implementation of the cyclic queue can refer to http://www.cnblogs.com/wolfred7464/p/4337093.html)

3. Create a Send thread and a receive thread to process messages in the queue.

4. When disconnecting, notify and wait for the send and receive threads to end, and then close the socket connection.

Why use a cyclic queue is to take into account the memory fragmentation and efficiency problems caused by the frequent request to reduce memory, and the queue intentionally does not design the dynamic growth of the function (add this function is simple, create a new node, call two times Set_next connection to go in), Custom socket framework is definitely to adapt to the needs of specific projects, the size of the socket buffer queue should be estimated in advance, if the queue is full, the asynchronous call will return an error code, this time should be considered to be off the net, or the program has a bug, and should not let the queue to dynamic growth.

Finally, the GitHub link is attached to the code, but the initial design is complete and the functionality will continue to improve. Https://github.com/Anti-Magic/AsyncSocket

Socket frame continuation, asynchronous socket

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.