Effectively, it looks like what you are trying to do is wrap some raw data you already have in a file in UDP or TCP and IP headers and then get the 'wrapped' data output? Note that this will be problematic for TCP since its a two way protocol and the 'wrapped' stream would be dependant on the responses from the remote site (re transmissions, packet sizes, etc)
One quick and dirty way to accomplish this would be to transmit the data to a remote host (could be nonexistant for UDP, though you'd need a listening socket on the remote for TCP. You could transmit this data via netcat or netcat6 for IPv6. Then you can run tcpdump or wireshark on the same machine to capture the full wrapped stream to a file in PCAP format. This PCAP would have your raw data packetized (data inside UDP/TCP, IP, and Ethernet headers)
It sounds like for your application, you would want to strip out the ethernet headers before using the data.
A previous answer to this question recommended hping, which would probably give you more flexability at the expense of a longer learning curve to get the results you want.