Java Socket specify a certain network interface for outgoing connections
This is the result of the 'weak end system model'. It's too broad to describe here but it is discussed in RFC 1122.
capturing both incoming and outgoing packets using raw socket
I have found this while searching over your problem. I haven't tried this. May be this will work.
int v=0;
v = PACKET_MASK_ANY & ~(1<<PACKET_OUTGOING) & ~(1 << PACKET_LOOPBACK);
setsockopt( raw_sock, SOL_PACKET, PACKET_RECV_TYPE, &v, sizeof(v));
Can Python select what network adapter when opening a socket?
On Windows, if you know the IP address of the interface you want to use, just bind to that before you connect. On Linux,use socket option SO_BINDTODEVICE as suggested by JimB (seems to be a privileged call too).
i.e. on Windows
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('192.168.0.1', 0))
s.connect(('...'))
Binding source address under Windows, selects the interface with the same IP address as that device, even if that IP address has a higher routing metric cost. This doesn't work under Linux though, as it always overwrites the source address with the IP address of the selected device. Routing is done based solely on the destination address. The only exception it seems is if you set source address to 127.0.0.1, then Linux prevents these packets from going out of that box.
Related Topics
Building Perf with Babeltrace (For Perf to Ctf Conversion)
What Is The Most Efficient Way to Exchange High Volume Data Between 2 Process
On Building Docker Image Level=Error Msg="Can't Close Tar Writer: Io: Read/Write on Closed Pipe"
The Difference Between Wait_Queue_Head and Wait_Queue in Linux Kernel
Poor Memcpy Performance in User Space for Mmap'Ed Physical Memory in Linux
How to Execute an Arbitrary Script with a Working Directory of The Directory Its In
How to Handle Bash with Multiple Arguments for Multiple Options
Grep and Sed with Spaces in Filenames
Can't Add File to Git Repository But Can Change/Commit
What Happens Internally When Deleting an Opened File in Linux
Linux/Unix Socket Self-Connection
In Shell, Split a Portion of a String with Dot as Delimiter
How to Check If One File Is Part of Other
Systemd/Udev Dependency Failure When Auto Mounting Separate Partition During Startup