"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
Previously, some code for network programming was written, but not very well written. To think carefully, or to understand the TCP/IP protocol is not enough. In addition to the recent realization of LWIP, learn the source code on GitHub, and then practice, there are many gains.
A) TCP is connection-oriented and UDP is non-connected. TCP's time-out retransmission and congestion control mechanism ensure the stability of TCP. For the complex network of the WAN, it is more insurance with TCP.
b) To achieve high-performance server programming, it is best to use epoll,socket non-blocking way, timely use heartbeat mechanism to detect the health of the socket;
c) socket read and write are to run continuously, even if the failure, such as the emergence of eintr, but also to continue to read and write down;
d) Send the packet to show the length of the packet description;
e) state machines and timers are always the correct protocol processing methods;
f) correctly handle multi-core socket representation method, can be used in Skynet way, can also be multi-threaded libevent way;
g) TCP is best closed by the client so that the server does not enter the timewait state;
h) LAN test results do not mean that the WAN is OK, so be sure to try in different scenarios;
i) the state switching mechanism of TCP should be in the heart;
j) Wireshark's clutch tool should be used frequently;
k) The data of the WAN must be encrypted;
L) Connect, read, write, close can all be resolved by asynchronous callback;
m) timer can be fully implemented by itself;
N) User-side checksum can be replaced by a more complex algorithm;
O) If there is no interaction between users, the HTTP proxy can also be used;
P) UDP is more suitable for LAN;
Q) Some real-time protocols such as RTSP, with UDP is also appropriate;
R) UDP Send packet, length is smaller, otherwise the IP layer forced subcontracting, it is easy to drop packets;
s) Invalid socket to be in time close, otherwise memory leaks;
T) efficient socket must involve buffer management, after all, many times packet is not finished, read;
u) emphasis on sequential operation, be sure to add your own SEQ and ack like TCP;
V) message transmission is best based on JSON format, not strict format, otherwise bad expansion;
W) The development of the business is generally based on a communication protocol, private or standard, these are not important, can have different version;
x) different development languages have their own framework, but understanding the fundamentals of TCPIP is more important;
Y) can implement a small TCPIP protocol stack to deepen the impression, read the lwIP classmate, certainly will not think this is a very scary thing;
z) socket + db is basically the main mode of server development, you can find a popular app on the market, imagine how their server-side design, practice practiced hand.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Random recording (some problems in network programming)