Solved

How do I specify/bind a socket to a specific NIC?

Posted on 2010-08-26
4
1,448 Views
Last Modified: 2013-11-29
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("164.217.14.10");
bind(my_udp_socket, (SOCKADDR*) &send_struct, sizeof(send_struct));

...where NIC 1 is 192.168.1.10 and NIC 2 is 164.217.14.10 (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 = ("164.217.255.255");
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?
0
Comment
Question by:JeepGuy
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:vanillasprinkles
ID: 33536979
you will probably be better off setting up two sin_addr's and the corresponding ports and binds for each address:port you want to have in your program (yes you can use send_struct for each unique ip:port you want but it can get messy and hard to keep track of)

I dont think it matters much, but in code i've written, my sendto does not use (const struct sockaddr *), but rather, (struct sockaddr *)
0
 

Author Comment

by:JeepGuy
ID: 33537182
I have already set up two different sockaddr_in structs for the different sockets.  I even tried using one for the bind and another for the sendto() func for the same socket.
0
 
LVL 3

Expert Comment

by:vanillasprinkles
ID: 33538473
sounds awful

oh, i did notice (forgot to add it to my first comment)

sin_addr.s_addr = inet_addr("IP")

your second sin_addr.s_addr=  does not have the inet_addr - not sure if you wanted it like that or not.


I have referenced this page (http://www.linuxhowtos.org/C_C++/socket.htm) many times in the past; what I would to is start a test project for yourself, with binding to two unique IP:Port's and exporting that working code to your project, so that you dont overwhelm yourself in your full sized project.
0
 

Accepted Solution

by:
JeepGuy earned 0 total points
ID: 33559995
yeah, that second sin_addr.s_addr= statement was a typo in my post.  I have it right in my code.

I have abandoned this effort after some more research.  From what I've gathered it's not possible to specify the interface in code.  I was able disable the second interface, multi-home the other interface, and get it working that way.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Provide internet access from one windows PC to another 16 97
Cisco router external connection issues. 6 30
Hidden network 2 39
Network adapter failed to start 5 33
Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
Data center, now-a-days, is referred as the home of all the advanced technologies. In-fact, most of the businesses are now establishing their entire organizational structure around the IT capabilities.
Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question