C # Implementation of multithreaded asynchronous socket packet receiver framework

Source: Internet
Author: User
Tags net thread socket thread

A few days ago in the Bo asked to see a C # Socket problem, think of the author 2004 do a provincial traffic flow receiving server project, the basic request is as follows:

Receive automatic observation equipment traffic packets reported through the wireless network card, Internet and socket the automatic observation equipment that runs all year round 365*24 report the observation data 5 minutes, each record approximately 2K size There will be 100 or so automatic observation equipment planned throughout the province (only 30 as of October 2008)

At that time, VS2003 was released more than a year, the author is also contact C # soon. So Google the domestic and foreign network, hope to find a point to apply C # to solve socket communication problems and code. Finally, found two articles to help the largest article: one is written by the Chinese socket receiver framework, the application of the independent client socket session (sessions) concept, to provide the author a general framework for the reception server ideas; another is written by the Americans, put forward the multithreading, The technical scheme of packet receiving packet, describes many implementation details of multithread and asynchronous socket, and this paper has determined the technical route of processing socket receiver with multithreading and asynchronous method.

The implementation and testing of the author also found that in the Internet environment in the socket application, the need for the system has a strong fault tolerance: no way to control the exception, you must allow them to exist (additional source code can be seen, try{}catch{} statements). In this respect, the author designed a special inspection and cleaning thread, complete invalid or timeout session cleanup and resource release work.

Vaguely remember that the domestic framework of the author's namespace has IBM, think of IBM employees, through the mail before they know that the person in Shenzhen. The author consulted several questions to him and discussed several technical key points with each other. Unfortunately, now to find, has not been found in the original text and mail. Have to take this opportunity, this document to the two never met the technical expert and peer, but also hope that the humble text or source can give readers a little useful inspiration and help.

1. Main technical Ideas

The entire system consists of three core threads, and consists of. NET thread Pooling Unified management:

Listens for client connection request threads: Listenclientrequest (), which loops listening for client connection requests. If so, detect the client IP to see if it is the same observation device, then establish a client TSession object and call the method asynchronously via the socket BeginReceive () receive packets, endreceive () processing packets Packet processing thread: handledatagrams (), cyclic detection packet queue _datagramqueue, complete packet parsing, judgment type, storage and other work client status detection thread: Checkclientstate (), loop Check client session Table _ Sessiontable, determine if the session object is valid, set timeout session shutdown flag, clear Invalid Session object and release its resources 2, main class introduction

The system consists of 3 main classes:

Tdatagramreceiver (Packet receiving server): The core process class of the system, establishing socket connection, processing and storing packets, cleaning up system resources, this class provides all public properties and methods TSession (client session): A socket object that consists of each client, has its own data buffer, and the cleanup thread determines whether to timeout Tdatagram (packet Class) based on the most recent session time of the object: sentence

Broken packet categories, parsing packets

3. Key functions and codes

The key implementation code for the core class Tdatagramreceiver is briefly shown below.

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.