來源:http://blog.sina.com.cn/s/blog_493309600100clrw.html
TCP與UDP差別
TCP---傳輸控制協定,提供的是面向連接配接、可靠的位元組流服務。當客戶和伺服器彼此交換資料前,必須先在雙方之間建立一個TCP連接配接,之後才能傳輸資料。TCP提供逾時重發,丢棄重複資料,檢驗資料,流量控制等功能,保證資料能從一端傳到另一端。
UDP---使用者資料報協定,是一個簡單的面向資料報的運輸層協定。UDP不提供可靠性,它隻是把應用程式傳給IP層的資料報發送出去,但是并不能保證它們能到達目的地。由于UDP在傳輸資料報前不用在客戶和伺服器之間建立一個連接配接,且沒有逾時重發等機制,故而傳輸速度很快
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.

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://www.cnblogs.com/heyonggang/archive/2013/03/08/2950096.html,如需轉載請自行聯系原作者