Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to deal with multi-IP on TCP/IP programming mode?

How to deal with multi-IP on TCP/IP programming mode?

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminjsonhelptutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeZhang
    wrote on last edited by
    #1

    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!

    C X 2 Replies Last reply
    0
    • J JoeZhang

      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!

      C Offline
      C Offline
      cmk
      wrote on last edited by
      #2

      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

      J 1 Reply Last reply
      0
      • C cmk

        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

        J Offline
        J Offline
        JoeZhang
        wrote on last edited by
        #3

        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!

        C 1 Reply Last reply
        0
        • J JoeZhang

          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!

          X Offline
          X Offline
          Xiangyang Liu
          wrote on last edited by
          #4

          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

          ](http://hometown.aol.com/XiangYangL/)

          1 Reply Last reply
          0
          • J JoeZhang

            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!

            C Offline
            C Offline
            cmk
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups