How do I specify/bind a socket to a specific NIC?
Posted on 2010-08-26
I'm trying to configure multiple sockets in one app and have them bound to specific addresses & NICs. I have one TCP socket setup on one address, but I need to set up a separate UDP socket for broadcasting to use a completely different address configured on the other NIC. I can't seem to specify the UDP socket to send FROM the second NIC. I've done the....
send_struct.sin_family = AF_INET;
send_struct.sin_addr.s_addr = inet_addr("18.104.22.168");
bind(my_udp_socket, (SOCKADDR*) &send_struct, sizeof(send_struct));
...where NIC 1 is 192.168.1.10 and NIC 2 is 22.214.171.124 (mask: 255.255.0.0)
But, that doesn't seem to do it. When I call my sendto() function.....
send_struct.sin_port = htons("3000");
send_struct.sin_addr.s_addr = ("126.96.36.199");
sendto(my_udp_socket, (int8 *)pkt, len, 0, (const struct sockaddr *)&send_struct, sizeof(send_struct));
...I don't see it show up on the wire (and the return value from sendto() returns the number of bytes I'm sending and I'm connected to an actual hub, not a switch...thus Wireshark shows everything). I've searched for an example on someone setting up multiple socket connections bound to different NICs/subnets and haven't found a solution that works yet. Any ideas?