Share soket development experience my socket-based persistent layer)

Source: Internet
Author: User
ArticleDirectory
    • Preface
    • Development Process
    • Summary
    • Follow-up
Preface

In this project, pixysoft. Framework. noebe. Sockets exposes database operations using socket to implement LAN calls.

Firewall traversal, lan-public network connection, and routing are not supported.

After the work is completed, we can use this framework to link a large number of heterogeneous databases into a large database pool (cloud ??)

 

Development Process

Phase 1 self-righteousness

At the beginning, I searched several socket LAN chat examples on the Internet and implemented them using the synchronous method.

Link here: http://www.pudn.com/downloads40/sourcecode/p2p/detail138986.html

In two days, I made a prototype, and there was no problem with the local call. I thought I was so lucky that I had a socket server in two days.

Result 1 is a problem.

The server is transmitting data, but it will be disconnected if it receives less than 10% locally. Waiting with a thread is just a fart. Who knows when the thread will wait. Even if the data offset is passed, it is broken.

 

The second stage is taken seriously

Suddenly frustrated, began to seriously search for information. In codeprojects and cnblogs find the asynchronous socket example, especially ncindy this is very good, there is a very simple asynchronous model example http://www.cnblogs.com/ncindy/archive/2006/11/01/547143.html

 

So the new solution:

1. Use Asynchronous Server

2. Use a custom end symbol, for example, \ 0 \ r \ n \ 0 \ r \ n. Let the socket read to determine the end. So we don't need to wait for a thread.

3. This ending symbol may be divided into two data packets. Therefore, the last packet must be merged for judgment.

It took another two days to build the system asynchronously. It was even better to run the system without waiting for thread. Thought it was OK. Then deploy and then collapse.

There is no problem with the large data volume transmitted to the server (about 5 MB), but when it is accepted, the server will show that the client is disconnected, and the client is still waiting.

 

Stage 3 Self-Development

Because the client does not know that the link is broken, I thought of using dual-link guarantees and implementing a resume mechanism. Is to locally send a command:

Resume; etid of the resume; offset

The notification of this socket will replace the failed socket and resume. Then, a heartbeat link is used for detection and a data link is used for transmission.

Deployment, tnnd, and crash again. When two links are disconnected at the same time, the system waits again.

 

Stage 4 completed

Continue to think, since there is a resume mechanism, why not make a timer by yourself. If the data is not accepted after this time, disconnect it and resume the data transfer.

Of course, I hate to use thread most and use Asynchronous timing.

1. First, there is a statistician to measure the transmission frequency.

2. asynchronous wait time = frequency * 10.

3. If the asynchronous wait is over and the data is still not received, the system will automatically disconnect and resume the data transfer.

Finished!

 

Phase 5 completed

After testing, in a super harsh environment (BT/eMule/Webpage/Tudou/and network disconnection from time to time), the system can achieve stable transmission of large data volumes.

 

Summary

1. socket server development, try to use Asynchronization, that feels super nice! I can view logs and process multiple links within 1 Millisecond (not simply receive, but process ). In this way, the throughput in one second is very large.

2. Use a custom terminator to indicate that the transmission is complete, for example, \ r \ n. Of course, \ 0 cannot be used.

3. The link is disconnected, and the client does not necessarily know it. It will enter the false state. Therefore, the client link needs to be viewed. We recommend that you use Asynchronous detection instead of threads.

4. At last, the socket transmission is extremely unstable. If the socket is disconnected, it is not a precursor.

 

Follow-up

Next, we plan to use Comet technology to implement real-time database calls across firewalls. Called

Pixysoft. Framework. noebe. comets

General idea:

1. There is a website as the Registration Center

2. After the server is started, link the website to implement comet

3. When the client needs to access the server database, it is connected to comet.

If you have the same research direction, I hope you will discuss it with each other.

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.