socket / UDP programming problem

I am programming a SNMP client that uses UDP protocol to communicate with SNMP agent (server). I need to use broadcast because the client does not know where the agent is.
If the client runs on a PC with single network adapter, it works well. But if there are two adapters on the PC and each of them have different network domain, e.g. and, the client can not find the agent because it sends the packet via a wrong adapter.
My program looks like following scenario:

socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
setsockopt (socket,SOL_SOCKET,SO_BROADCAST,...);
recvfrom (socket,...);

Any comments are welcome.
Who is Participating?
sk33v3Connect With a Mentor Commented:
You need to call the bind method to bind your socket to a specific Local IP Address.
Justin_HuangAuthor Commented:
Thank you but how could I get the local IP address if there are more than one adapter?
Further more, how could I know which one is the correct one that the socket should be bind with?
At some point someone has to decide which IP address to bind to. You can enumerate through all the local IP Addresses on the machine and allow the end user to pick one. Do you need code to show you how to enumerate through the IP's?
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Justin_HuangAuthor Commented:
Actually, my SNMP agent is just a function of my printer driver. I'm using the SNMP protocol to search available printers on the network. It is not a good idea to ask the end user to pick the IP address. Instead, the driver should make the decision by itself.

I am not sure if there is any way to call the sendto() only once and have the packet sent through all the network cards. If there is such way, my program would become easier. Otherwise, I have to create multi-thread architecture for handling multiple network cards.

I'm not familiar with socket programming. I would very appreciate if anyone could tell me what kind of Windows APIs should be used. That would shorten my time trial and error.
Now I increase the point value from 125 to 250 in order to get more help.
Hmm, ok you said you are using the Printer driver to accomplish this task. What printer driver is it? Can you send a link to the documentation?
Justin_HuangAuthor Commented:
The printer driver is under construction and it would involve some confidential issues if I tell you the printer model. The printer we are developing is a network printer that supports SNMP and printer MIB. What I need to do is to provide a function for end user to find available printers on the LAN. This function is also necessary for the driver installer.
Any ideas?
Hmm, what I would probably do is bind one UDP Socket to each IP Address on the machine. Then I would send out the SNMP packet and list any returned items. So instead of just sending it on one network I would suggest just sending it on all networks. Let me see if I have a sample of enumerating the IP addresses somewhere around here.
Justin_HuangAuthor Commented:
I have found the APIs for enumerating the IP addresses on local machine.
You are right that I need to bind the socket to each IP address.
Thank you very much for the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.