How to deal with multi-IP on TCP/IP programming mode?
-
Hi… there, I wonder how you guys deal with the case of more than one available IP address (multi-IP) on the local host on the TCP/IP programming mode. I'm talking about the "multi-IP" here, it's usually the system which installed with more than one network adapter (physical or virtual), i.e., more than one available IP address… In the case, when you call the socket API "bind" to bind your socket with the local host, you must choose the proper IP address from the multiple IP addresses, otherwise, your socket possibly cannot work. So, my problem is: how do you know which IP address is available on that LAN? The current solution I'm using is: Present a dialog with a list control, and all the available local IP addresses listed on the list control, then ask the user select the proper one that my program would use... :) Does anyone here can tell another better solution to this problem? i.e., how does my program automatically detect all the IP addresses, then automatically pick up the proper one, instead of asking the user for this? Thanks!
-
Hi… there, I wonder how you guys deal with the case of more than one available IP address (multi-IP) on the local host on the TCP/IP programming mode. I'm talking about the "multi-IP" here, it's usually the system which installed with more than one network adapter (physical or virtual), i.e., more than one available IP address… In the case, when you call the socket API "bind" to bind your socket with the local host, you must choose the proper IP address from the multiple IP addresses, otherwise, your socket possibly cannot work. So, my problem is: how do you know which IP address is available on that LAN? The current solution I'm using is: Present a dialog with a list control, and all the available local IP addresses listed on the list control, then ask the user select the proper one that my program would use... :) Does anyone here can tell another better solution to this problem? i.e., how does my program automatically detect all the IP addresses, then automatically pick up the proper one, instead of asking the user for this? Thanks!
What do you mean by the 'proper one'. Usually it is sufficient to use ADDR_ANY. This allows you to listen on all local address. From MSDN docs for bind() : If an application does not care what local address is assigned, specify the manifest constant value ADDR_ANY for the sa_data member of the name parameter. This allows the underlying service provider to use any appropriate network address, potentially simplifying application programming in the presence of multihomed hosts (that is, hosts that have more than one network interface and address). ...cmk Rosencrantz: I don't believe in it anyway. Guildenstern: What Rosencrantz: England Guildenstern: Just a conspiracy of cartographers then. Save the whales - collect the whole set
-
What do you mean by the 'proper one'. Usually it is sufficient to use ADDR_ANY. This allows you to listen on all local address. From MSDN docs for bind() : If an application does not care what local address is assigned, specify the manifest constant value ADDR_ANY for the sa_data member of the name parameter. This allows the underlying service provider to use any appropriate network address, potentially simplifying application programming in the presence of multihomed hosts (that is, hosts that have more than one network interface and address). ...cmk Rosencrantz: I don't believe in it anyway. Guildenstern: What Rosencrantz: England Guildenstern: Just a conspiracy of cartographers then. Save the whales - collect the whole set
cmk wrote: ...This allows you to listen on all local address... Well, if you just "listen" and not "send" data,or the local host has ONLY one IP address, it would be fine. But by this way, if the local host has more than one IP address and you want to send data to the other host, it may not work sometimes, why? because "the underlying service provider" sometimes would automatically bind an "exist-but-not-connect" IP (though a host have several IP addresses, maybe only one or some connectable and others don't), and, of course, you cannot send data outside successfully via it... and this is approved in my current application :) What I mean "proper one" is: you cannot alway hope that the ADDR_ANY would do the right thing for you, you must find out the working and connectable IP address, and then bind your socket to it. :sigh: Thank you!
-
Hi… there, I wonder how you guys deal with the case of more than one available IP address (multi-IP) on the local host on the TCP/IP programming mode. I'm talking about the "multi-IP" here, it's usually the system which installed with more than one network adapter (physical or virtual), i.e., more than one available IP address… In the case, when you call the socket API "bind" to bind your socket with the local host, you must choose the proper IP address from the multiple IP addresses, otherwise, your socket possibly cannot work. So, my problem is: how do you know which IP address is available on that LAN? The current solution I'm using is: Present a dialog with a list control, and all the available local IP addresses listed on the list control, then ask the user select the proper one that my program would use... :) Does anyone here can tell another better solution to this problem? i.e., how does my program automatically detect all the IP addresses, then automatically pick up the proper one, instead of asking the user for this? Thanks!
I haven't tried this myself: you can probably use the gethostbyname function to return a HOSTENT structure, the returned structure contains a list of null terminated ip addresses. Good luck.[
My articles and software tools
-
cmk wrote: ...This allows you to listen on all local address... Well, if you just "listen" and not "send" data,or the local host has ONLY one IP address, it would be fine. But by this way, if the local host has more than one IP address and you want to send data to the other host, it may not work sometimes, why? because "the underlying service provider" sometimes would automatically bind an "exist-but-not-connect" IP (though a host have several IP addresses, maybe only one or some connectable and others don't), and, of course, you cannot send data outside successfully via it... and this is approved in my current application :) What I mean "proper one" is: you cannot alway hope that the ADDR_ANY would do the right thing for you, you must find out the working and connectable IP address, and then bind your socket to it. :sigh: Thank you!
Ok, so the only case you are looking at is creating a socket to use as a client that you can use send() on. Although i haven't tried it, you may want to try creating the socket, don't bind it, and then use sento() as the first send, as this performs an implicit bind on the socket. After that you should be able to use send(). I would assume that the implicit bind() would bind to an address that allows a connection to the specified node/port i.e. is 'live'. ...cmk Rosencrantz: I don't believe in it anyway. Guildenstern: What Rosencrantz: England Guildenstern: Just a conspiracy of cartographers then. Save the whales - collect the whole set