TCP與UDP區別

TCP與UDP區別

TCP---傳輸控制協議,提供的是面向連接、可靠的字節流服務。當客戶和服務器彼此交換數據前,必須先在雙方之間建立一個TCP連接,之後才能傳輸數據。TCP提供超時重發,丟棄重複數據,檢驗數據,流量控制等功能,保證數據能從一端傳到另一端。
UDP---用戶數據報協議,是一個簡單的面向數據報的運輸層協議。UDP不提供可靠性,它只是把應用程序傳給IP層的數據報發送出去,但是並不能保證它們能到達目的地。由於UDP在傳輸數據報前不用在客戶和服務器之間建立一個連接,且沒有超時重發等機制,故而傳輸速度很快

Overview

TCP (Transmission Control Protocol) is the most commonly used protocol on the Internet. The reason for this is because TCP offers error correction. When the TCP protocol is used there is a "guaranteed delivery." This is due largely in part to a method called "flow control." Flow control determines when data needs to be re-sent, and stops the flow of data until previous packets are successfully transferred. This works because if a packet of data is sent, a collision may occur. When this happens, the client re-requests the packet from the server until the whole packet is complete and is identical to its original.

UDP (User Datagram Protocol) is anther commonly used protocol on the Internet. However, UDP is never used to send important data such as webpages, database information, etc; UDP is commonly used for streaming audio and video. Streaming media such as Windows Media audio files (.WMA) , Real Player (.RM), and others use UDP because it offers speed! The reason UDP is faster than TCP is because there is no form of flow control or error correction. The data sent over the Internet is affected by collisions, and errors will be present. Remember that UDP is only concerned with speed. This is the main reason why streaming media is not high quality.





On the contrary, UDP has been implemented among some trojan horse viruses. Hackers develop scripts and trojans to run over UDP in order to mask their activities. UDP packets are also used in DoS (Denial of Service) attacks. It is important to know the difference between TCP port 80 and UDP port 80. If you don't know what ports are go here.

Frame Structure

As data moves along a network, various attributes are added to the file to create a frame. This process is called encapsulation. There are different methods of encapsulation depending on which protocol and topology are being used. As a result, the frame structure of these packets differ as well. The images below show both the TCP and UDP frame structures.

TCP FRAME STRUCTURE

UDP FRAME STRUCTURE



The payload field contains the actually data. Notice that TCP has a more complex frame structure. This is largely due to the fact the TCP is a connection-oriented protocol. The extra fields are need to ensure the "guaranteed delivery" offered by TCP.

UDP
UDP 與 TCP 的主要區別在於 UDP 不一定提供可靠的數據傳輸。事實上,該協議不能保證數據準確無誤地到達目的地。UDP 在許多方面非常有效。當某個程序的目標是儘快地傳輸儘可能多的信息時(其中任意給定數據的重要性相對較低),可使用 UDP。ICQ 短消息使用 UDP 協議發送消息。
許多程序將使用單獨的TCP連接和單獨的UDP連接。重要的狀態信息隨可靠的TCP連接發送,而主數據流通過UDP發送。

TCP

TCP的目的是提供可靠的數據傳輸,並在相互進行通信的設備或服務之間保持一個虛擬連接。TCP在數據包接收無序、丟失或在交付期間被破壞時,負責數據恢復。它通過爲其發送的每個數據包提供一個序號來完成此恢復。記住,較低的網絡層會將每個數據包視爲一個獨立的單元,因此,數據包可以沿完全不同的路徑發送,即使它們都是同一消息的組成部分。這種路由與網絡層處理分段和重新組裝數據包的方式非常相似,只是級別更高而已。
爲確保正確地接收數據,TCP要求在目標計算機成功收到數據時發回一個確認(即 ACK)。如果在某個時限內未收到相應的 ACK,將重新傳送數據包。如果網絡擁塞,這種重新傳送將導致發送的數據包重複。但是,接收計算機可使用數據包的序號來確定它是否爲重複數據包,並在必要時丟棄它。

TCP與UDP的選擇

如果比較UDP包和TCP包的結構,很明顯UDP包不具備TCP包複雜的可靠性與控制機制。與TCP協議相同,UDP的源端口數和目的端口數也都支持一臺主機上的多個應用。一個16位的UDP包包含了一個字節長的頭部和數據的長度,校驗碼域使其可以進行整體校驗。(許多應用只支持UDP,如:多媒體數據流,不產生任何額外的數據,即使知道有破壞的包也不進行重發。)
很明顯,當數據傳輸的性能必須讓位於數據傳輸的完整性、可控制性和可靠性時,TCP協議是當然的選擇。當強調傳輸性能而不是傳輸的完整性時,如:音頻和多媒體應用,UDP是最好的選擇。在數據傳輸時間很短,以至於此前的連接過程成爲整個流量主體的情況下,UDP也是一個好的選擇,如:DNS交換。把SNMP建立在UDP上的部分原因是設計者認爲當發生網絡阻塞時,UDP較低的開銷使其有更好的機會去傳送管理數據。TCP豐富的功能有時會導致不可預料的性能低下,但是我們相信在不遠的將來,TCP可靠的點對點連接將會用於絕大多數的網絡應用。

http://blog.sina.com.cn/s/blog_493309600100clrw.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章