Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-26
4
Medium Priority
?
1,457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Turn your laptop into a mobile console!

The CV211 Laptop USB Console Adapter provides a direct Laptop-to-Computer connection for fast and easy remote desktop access with no software to install.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

670 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