?
Solved

socket / UDP programming problem

Posted on 2006-05-26
8
Medium Priority
?
341 Views
Last Modified: 2010-04-17
Hi,
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. 192.168.1.2 and 10.6.111.2, 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,...);
sendto(socket,...);
recvfrom (socket,...);

Any comments are welcome.
0
Comment
Question by:Justin_Huang
  • 4
  • 4
8 Comments
 
LVL 9

Accepted Solution

by:
sk33v3 earned 1000 total points
ID: 16769121
You need to call the bind method to bind your socket to a specific Local IP Address.
0
 
LVL 1

Author Comment

by:Justin_Huang
ID: 16770147
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?
0
 
LVL 9

Expert Comment

by:sk33v3
ID: 16770722
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Justin_Huang
ID: 16773518
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.
0
 
LVL 9

Expert Comment

by:sk33v3
ID: 16775640
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?
0
 
LVL 1

Author Comment

by:Justin_Huang
ID: 16777665
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?
0
 
LVL 9

Expert Comment

by:sk33v3
ID: 16778143
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.
0
 
LVL 1

Author Comment

by:Justin_Huang
ID: 16781867
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Screencast - Getting to Know the Pipeline

850 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