Simplest tool i could find for such task is linux network packet capture tool called tcpdump.
It can capture packets coming into local network device (for example: computer connected to the Internet) then display them or save to file. Such data can then be analyzed, for example to detect suspicious packets, or to debug internet protocols.
Or its source code can be examined and analyzed for inspiration, then custom version can be made.
But not without crediting authors.