Nethogs might be what you want.
debian/ubuntu/etc
apt-get install nethogs
or rhel/cent/etc
yum install nethogs
You run it on the network interface, e.g.
nethogs eth0
However, its output is bandwidth per process, sent and received. You can also monitor multiple interfaces.
DESCRIPTION
NetHogs is a small 'net top' tool. Instead of breaking the traffic down
per protocol or per subnet, like most such tools do, it groups band‐
width by process - and does not rely on a special kernel module to be
loaded. So if there's suddenly a lot of network traffic, you can fire
up NetHogs and immediately see which PID is causing this, and if it's
some kind of spinning process, kill it.
UPDATE: OP asked for output to file. You can do it with nethogs:
nethogs -d 1 eth0 > output.txt
-d specifies the time interval in between writes... so you could do the above command and get the desired result.
UPDATE2 OP says TCP and UDP is a requirement. Then attempt to use jnettop. A bit older, but does TCP and UDP. It is available in deb/ubuntu and I think RHEL derivatives. example:
jnettop --display text -t 5 --format CSV
This should be pipe-able to a file.