TCP與UDP區別,TCPUDP區別
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 STRUCTUREUDP 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可靠的點對點連接將會用於絕大多數的網路應用。