I have developed a common data transmission module in my work, and the followers of C ++ are growing fast. I will share some experiences in this article.
1. Basic Concepts
Create three handshakes for TCP connections and disconnect four handshakes for TCP connections.
2. troubleshooting tools:
1). netstat-anp | grep your's Port:
This command is used to check the status of your port in Linux. It can be used to check whether the connection is established and normal (the status is established, the sending and receiving stacks are empty, or the status remains unchanged) whether to disable the server firewall (the firewall of the machine is often not stopped, and the request connection status is in SYS send ).
2) tcpdump, an artifact for viewing port data circulation
For basic usage, please google and learn several key points: How to check whether a package is a push package or an answer package, how to analyze the network connection status, and how to save ABC. pcap files can then be analyzed using wirshark on the Windows platform (this must be mastered, usually capture packets to check whether data transmission is normal );
3). basic steps:
First use 1) to check whether the network connection is normal. If 1) The problem cannot be found, use 2) packet capture analysis, and review your code in detail. The logic is good.
3. Program Model:
Another topic that can't be left without network programming is multithreading. In real scenarios, almost all data is sent and received in parallel, so the program model is crucial. A good program model can not only greatly improve the processing speed, but also shorten the R & D cycle.
When thinking about the model, we mainly consider the following points:
1) whether the IO type is NIO or bio. Different Types determine different thread models.
Bio corresponds to a thread and a connection, so when there are a large number of connections (each connection should have its own cache memory block and a public large memory, however, you must use a read/write lock to process data access. When there are too many connections, even the read/write locks may not be able to meet the requirements, so it is worthwhile to spend more memory ), one consumption thread is required to manage the data pushed by several connections.
NIO copies the received data to a distribution thread first, and then the distribution thread distributes the data evenly to N consumption threads, and uses parallelization to increase the processing rate.
2). number of TCP connections, validity period of each connection, and data volume of each connection
These are the important bases for determining the type of I/O to use. If it is a persistent connection with high real-time data requirements and a large amount of data, bio; if the number of connections is particularly large (500 +), select NiO.
3). Learn to draw model diagrams and write design documents. Queues between classes should not be exposed (hold object references and call corresponding methods to push/POP elements)
4). Replace the lock in the key points. For a lock replacement idea, refer to my bloghttp: // www.cnblogs.com/uttu/archive/2013/02/07/2908685.htmlin several key logic points.