At first, I thought most of the previous answers did not answer the question. After further reflection, I think the author probably does not ask the right question.
If you are writing an application, you usually do not think about "creating packages." you just open sockets, format the data payload, and this is the protocol stack that creates packets with your data. Well, if you use datagrams, you need to identify, generate and analyze your payloads. But you usually allow the kernel to encapsulate it at the network level (for example, add an IP header) or the link layer (for example, add Ethernet framing). Usually you do not use pcap. Sometimes just pack and unpack and maybe enough vec.
If you are writing an unusual batch processor, such as an active tool of an aggressive attack, a person in the middle process, or a traffic shaping device, you are likely to โcreate packetsโ and use pcap, Net :: Packet may also be for you.
source share