当前位置:网站首页>Protocol selection of mobile IM system: UDP or TCP?

Protocol selection of mobile IM system: UDP or TCP?

2022-06-26 05:06:00 JackJiang-

1、 Preface

For developers with experience in network programming , What kind of data transport layer protocol is used to realize data communication , It's a very basic question , It involves your first line of code  How to write .

from PC In the era of IM Start ,IM Developers are arguing about the choice of data transmission protocol ( such as :《 Why? QQ It's using UDP Agreement, not TCP agreement ?》 Questions like this , You can see it in the community every once in a while ). In the era of mobile Internet , In view of the unreliability of mobile network and other characteristics , Plus the power saving strategy of mobile phones 、 Flow compression, etc , Add more uncertainties to the answer to this question .

For people with a choice difficulty card , Based on the above factors , add UDP and TCP The essential difference of the agreement , It's a tangled choice . This article will summarize from the author's practice , Give a self-contained point of view , If you have any objection, please reply rationally , Not for spray , For reference only .

explain : This article quotes DDPush Technical data of , Thanks to the original author . ( This article is published synchronously in :http://www.52im.net/thread-33-1-1.html

2、 Exchange of learning  

- Instant messaging development communication group : 215891622 [ recommend ]

- Mobile IM Develop recommended articles :《 Beginner level one is enough : Develop mobile from scratch IM

3、 Reference material

Why? QQ It's using UDP Agreement, not TCP agreement ?
UDP What is the maximum size of a package in
be based on TCP The mobile end of the protocol IM We still need a heartbeat mechanism
NAT Detailed explanation : The basic principle 、 Crossing technology (P2P Hole digging )、 Port aging, etc
Computer network communication protocol diagram ( Chinese Collection Edition )
Theoretical Classics :TCP Agreed 3 Shake hands with 4 Detailed explanation of the wave process
Technical test and analysis of the influence of wechat on the network ( Paper full text )

4、UDP vs TCP

TCP still UDP? How to realize long connection ? How to realize heartbeat mechanism ? How to determine the interval of heartbeat ? These questions are all for discussion  Mobile IM、 News push and other similar topics , Almost certainly asked questions . Let's try to be honest .

5、 Internet 、 Mobile Internet network environment

In the end, we should use UDP still TCP Before , It is necessary to discuss the characteristics of the network environment of the Internet and mobile Internet .

Network infrastructure of Internet , After more than ten years of long-term development , Has been more stable and mature ,PC terminal 、 The ability of the operating system has also reached a high level .

And mobile Internet , Because it involves the base station of wireless telephone network 、2G、3G and 4G The continuous development of technology , Its stability 、 bandwidth 、 Although resource allocation and other aspects are becoming more and more perfect , But there are still many problems at present . in addition , Because of the mobile Internet “ Move ” The essence of , Plus intelligent terminal devices ( A smart phone 、 The tablet ) Later development , What is still evolving , Compared with the Internet , Mobile Internet or low speed 、 unstable 、 When the terminal capacity is a little weak . And because of it “ Move ” The essence , It's hard to achieve the quality of the Internet in a short time .

therefore , In the Internet Environment , Network applications due to network facilities 、 The maturity of the operating system , It's easier to develop and use , Resources are more abundant . And the mobile Internet still has to “ square accounts in every detail ”.

6、 Battery life of intelligent terminal , System sleep

Battery life of intelligent terminal equipment is always a technical bottleneck . In case of continuous use , Most smart device batteries can't support more than two hours . So when there is no external power supply , Intelligent terminal devices must be frequent 、 Long sleep , This will greatly affect the network application scenarios in two network environments .

7、IPv4 resources 、 Port resources

This topic is often ignored by many people , But it has a crucial impact . Although most people are very clear IP The dynamics caused by the lack of address IP The inevitability of distribution , But ignore because of IP Insufficient port resources due to insufficient address .

Because of the need for dynamic allocation IP Address ( This is not just about internet access IP, It also includes IP), Routers work by port mapping , Put the internal network ( Include PC、 mobile phone 、 Flat 、Wifi、2G、3G、4G)IP Map to external with port IP( It's usually the public network IP) And the corresponding port , And maintain this mapping , In order to modify 、 Forward message information , Make sure it's all inside ip、 Ports and external ones ip、 Port communication .

However , Single IP The port resources of the address are limited , The theoretical upper limit is 65535 Ports . For a normal broadband router , This is enough . however ! For large network services 、 Network backbone access point, etc , If IP Insufficient resources , Every IP Tens of thousands of ports will soon run out of resources , So as to affect normal communication .

8、 Port mapping aging time

Because of that , All routers will set the aging time for each port mapping relationship , If the aging time is countdown to 0, Then the port mapping relationship fails , The port is released for use by other connections . If all ports are exhausted , You can no longer create a new internal and external network connection .

Port mapping aging time , Much shorter than many people think . General home broadband router , The aging time is usually two or three minutes ; In the access part of cable broadband operators , Aging time may be less than two minutes . In the wireless telephone network operator access part ( for example GPRS Connect ), The aging time is even less than one minute !

in other words , Any network communication ( Whether it's TCP or UDP), If there is no network message transmission within a few minutes , What it occupies IP The address port will be recycled by the router . At this time, the communication will be terminated , No matter TCP still UDP, Everything is nothing .

The crueler truth is , The Internet can be thought of as being connected by countless routers , A network communication often needs to go through n Router , Each router will set up its own port mapping for one communication . As long as one of the routers reclaims its port , Then the whole communication is interrupted .

That's why many people wonder TCP Of KeepAlive Parameter cannot guarantee the reason for long connection .TCP Of KeepAlive The default is two hours ( And the parameter is TCP Optional implementation of , Not necessarily ), Under the influence of router port mapping aging time , It's not going to work . actually , This parameter can only be used in a single LAN , It also depends on the specific operating system .

Due to the existence of router port mapping , Plus smart terminals are frequent 、 Long sleep ,TCP The practicability of long connection is greatly reduced in the case of mobile Internet .

And because of that , Mobile IM、 The push system must implement the so-called heartbeat packet mechanism , To maintain the aging time of the port mapping relationship will not be reduced to 0 And being recycled , So as to avoid connection interruption .

9、 Server bearing capacity

Whether it's UDP still TCP, In the end, it is the device of the application server to provide services . and TCP Because it provides secure and reliable streaming services , It's about computers 、 The consumption of network resources is far greater than UDP Agreed . For mainstream servers with better configuration , With a lot of memory ( Dozens of G Up to 100 G Memory ), With high-speed disks 、 network card , Can support millions of TCP Connected . But we need more professional server settings , Many system parameters need to be adjusted , Plus the service program . in addition ,TCP Connection establishment 、 Maintenance and release , They all need more expensive calculations 、 Network resources .

Terminal online services , If it's a simpler service , It is not necessary to use TCP Many advanced functions , But bear TCP The high cost of , Not necessarily worth it . If it works UDP To provide services , The capacity of a single server , Yes, we can TCP Dozens of times the service , Even a hundredfold increase . That's why DNS This huge number of concurrent servers provide UDP Reason for interface .

in addition , Millions TCP Connected network services , The difficulty of programming 、 Program complexity 、 Debugging difficulty 、 Server O & M cost 、 The cost of network is much higher than UDP.

and UDP Programming , The difficulty and cost of communicating with millions of terminals is much lower . If the network service provided is not a flow based service , A certain probability of failure is also allowed ( for example P2P), be UDP It's often the more appropriate way .

10、 Advanced application network communication requirements

however , Mobile IM System 、 On the one hand, push system provides terminal online service , On the other hand, we need to consider the integrity and security of content information . After all, the loss of information , Or the wiretapping of Communications , It's hard to accept . and TCP No matter in the network layer reliability control , Or in the application layer security support ( for example HTTPS), They all provide powerful functions and convenience for applications that cannot be replaced .

11、 Conclusion

Combine the above , In fact, the answer is coming out .

Mobile terminals today IM、 Push system , Facing the uncertainty of mobile Internet , And face the frequent system sleep of intelligent terminal 、 Network switching , Also consider the bearing cost of the server , For online services UDP It's better than TCP A more appropriate way . But because of data integrity 、 The need for security , We should not give up TCP The reliability and safety of .

therefore , Personally think that , The more appropriate way is : Two communication protocols are used at the same time , Each has focused on .UDP To keep a large number of terminals online and under control , Application and business are through TCP To achieve . This and FTP Separation of service control and data , Take different connections , There are similarities and differences .

in fact , This is also the instant messaging giant QQ In the way . In the early days ,QQ Or mainly use TCP agreement , And then it turned to adoption UDP The way to stay online ,TCP To upload and download data . Now? ,UDP yes QQ The default way of working , To perform well . I believe this is also used on wechat .

Simple textual research : Sign in PC edition QQ, Turn off the extra QQ The window leaves only the main window , And minimize it . A few minutes later , View system network connections , Will find QQ The process no longer holds any TCP Connect , But there are UDP Internet activities . It's sending chat messages , Or open other windows and functions , Will find QQ The process will enable TCP Connect .

( This article is published synchronously in :http://www.52im.net/thread-33-1-1.html

author :Jack Jiang ( Click the author name to enter Github) 
Source :http://www.52im.net/space-uid-1.html 
communication : Welcome to the instant messaging development exchange group  215891622 
Discuss :http://www.52im.net/ 
Jack Jiang At the same time 【 original Java Swing Appearance engineering BeautyEye】 and 【 Lightweight mobile instant messaging framework MobileIMSDK】 The author of , You can go to download and exchange .
This blog post Welcome to reprint , Reprint please indicate the source ( You can also go to   my 52im.net  Find me ). 

原网站

版权声明
本文为[JackJiang-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180507139390.html