The terms packet and datagram are often used in relation to file transfer protocols, particularly TCP and UDP. What is the difference between the two?
A network packet is a unit of data that is formatted and carried by a network that is packet switched. It commonly consists of control information, which are the command sets, sequencing information and error detection codes for a given form of data transfer, and user data, which is commonly referred to as the payload. Control information is usually found in the packets trailers or headers.
A datagram is the basic unit of data transfer that is linked to a network that is packet switched. The common datagram is structured to hold payload and header sections, allowing them to provide a communication service that is virtually connectionless across its designated packet switched network.
Why would they be compared to each other?
These two terms are often compared to each other because they are sometimes used interchangeably and because of their relation to TCP and UDP. UDP transfers data in datagrams, which makes it fast, though unreliable and not secure, while TCP transfers data in packets which makes it a bit slow but reliable and secure, with measures of data correction and recovery.
The main difference between the two would be their reliability in transferring data over a given network. The arrival time, destination and order of packets is guaranteed and safeguarded, with data recovery measures in place, while in the case of datagrams, all these factors cannot be guaranteed by the network neither can any data lost be recovered by a user on any end.
The term datagram is often used synonymously to packets in order to refer to unreliable packets. These are delivery packets that either do not notify the sender if data is lost or distorted, or packets that may arrive at the destination user in a distorted order from the one specified by the sender. On the other hand, packets are termed to be reliable, error checked and secure, while reaching the intended user in a pre-determined order.
What is a datagram packet used for?
Datagram packets are usually used in implementing a packet delivery service that is connectionless. Each message in the service is routed between machines based on the information that is contained within that individual packet. The implication of this being that packets sent from the same machine to another one may be routed in different ways and arrive in any order.