TCP stream vs UDP message

后端 未结 6 812
悲哀的现实
悲哀的现实 2020-11-29 01:45

TCP is stream oriented meaning data is transferred as a continues stream of bytes. But what confuses me is that TCP creates segments and passes this down to IP. IP creates p

6条回答
  •  盖世英雄少女心
    2020-11-29 02:00

    Lot of confusion here. Let me clarify.

    TCP/IP is a stream-oriented, Packet and Connection oriented protocol. UDP is just a packet-oriented protocol. Doesn't establish connection first.

    Let us say that you are you using a Java program to connect to a network in your application by calling java.net.Socket class on the client side and java.net.ServerSocket on the server side. Once the connection is established the data transmission starts. The question comes, is the data sent in stream (Codata or infinite stream) or packet if I chose TCP? The answer is data received by the TCP method is stream but TCP converts stream into packet before sending the lower lavel stack. Basically, the application layer above sends the data in stream to the TCP layer and TCP breaks it down into packets to the network layer, and performs packet-to-streaming while receiving fro the server (receiving) side because your application Java can understand only Stream. File transmission is preferred via TCP over UDP because you can't afford losing of packets.

    UDP, on the other hand, is a packet-oriented protocol where the application such as Java class java.net.DatagramPacket; java.net.DatagramPacket; import java.net.DatagramsSocket creates a packet first before talking to UDP, and the packet is sent out with additional information by UDP/IP protocols to the server side. Note, some applications might present the data as a stream when the underlying protocol is UDP. However, this is the layering of an additional protocol on top of UDP, and it is not something inherent in the UDP protocol itself. Live straming of TV is generally UDP because you are not worried about loss of packets.

提交回复
热议问题