Send an asynchronous TCP development library for criticism, correction, and bug

Source: Internet
Author: User

Socket development is often used.ProgramIt may be very troublesome to handle network exceptions, to achieve asynchronous performance improvement, to handle sticky packets, subcontracting, analysis and processing protocols, it would be very convenient to have a development library like Mina in Java, so I wrote this thing first and only implemented the TCP protocol, ask for criticism and correction when the problem is still corrected, so that you do not need to adjust your head in the future. The Project address is http://edas.codeplex.com /.

Because it is very simple, there are only a few classes, so we will not pretend to be sending class diagrams. The basic principle is as follows:

An acceptor class is used to encapsulate socket listening operations. Here I provide two synchronous and asynchronous listening methods. According to experts on the Internet, the success rate of synchronous listening is high and the efficiency is not inferior to that of asynchronous listening, after testing, we found that the efficiency was indeed the same, so we added the synchronous listening method.

An asyncsocket class encapsulates asynchronous socket operation methods and events, including sending, receiving, and connection. To achieve asynchronous sending and receiving, it is sent to the sending queue first, in this way, the sending and receiving threads are opened independently, and each thread is in a different iocp completion event. A processor can be inserted into an instance of asyncsocket, and the processor object is self-nested. Multiple processor objects can form a processor chain and process data layer by layer, solve the Problem of sticking packets and subcontracting at this level.

I have implemented three most basic processors. One is a handshake processor, which is used to process handshake information and the other is a processor that splits data packets by separators, the last one is to split the data packet processor by defining the data packet structure. If you need to process your own protocol, such as HTML protocol, you only need to implement the iprocessor interface by yourself, and insert the processor into the tokenprocessor object.

I implemented a string line echo server in the source code, and sent the \ n string to it after connection. The server will send the source string back intact.

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.