none
TcpClient and 2 network cards RRS feed

  • Question

  • It there a good way to use TcpClient with 2 network cards?  When you create a new TcpClient how does it know which card to pick?
    • Edited by MichaelBarb Thursday, October 4, 2012 7:52 PM
    Monday, October 1, 2012 4:03 PM

Answers

  • If the IP addresses and the Mask are set properly on the network cards your software should not have to do anything to determine which card should be used.  It becomes automatic.  Let me give an example if you have two network cards like below

    Card 1 : 192.1.1.1  Mask 0.0.0.0

    Card 2 : 193.1.1.1 Mask 255.0.0.0

    Card 1 is the default gateway computer because it has a mask of 0.0.0.0.  Card 2 is the gateway to subnet 193.X.X.X.  Here are some common rules that should be used when configuring your cards

    1) Only one card should have a mask of 0.0.0.0

    2) The card with the mask of 0.0.0.0 should point to the outside world which is the computer with internet access.

    3) No two cards on the same PC when you AND the IP address and Mask together should have exactly the same results.  You wouldn't have the following tow cards configured in the PC

         Card A : 196.10.1.1  Mask 255.0.0.0       This would result with 196.0.0.0

         Card B : 196.20.1.1  Mask 255.0.0.0       This also would result with 196.0.0.0

    4) The card chosen to send a message is the route that is most restrictive route which is determined by the mask.  A route of a mask 255.255.255.0 will be used before a mask of 255.255.0.0.


    jdweng

    Tuesday, October 2, 2012 12:10 AM

All replies

  • TcpClient will use the normal IP Routing configuration of the system, and doesn't provide a direct way to control or override this.

    If you need to bind to a specific card, you can do it by setting the local address directly and using a Socket instead of TcpClient.  For details, see: http://stackoverflow.com/a/49545/65358


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, October 1, 2012 4:14 PM
    Moderator
  • I had seen the stackoverflow thread and was hoping there was a better way than using Sockets directly.  That article came from 2008 and I was hoping things had been improved by now.

    Is there a way to set "normal IP rooting" to decide which card to use?  How does the software determine what card is "normal"?  If it is part of computer configuration it seems like it goes to the first one installed.  I cannot find any settings for this.

    The article referenced is just about general network routing and does not talk about programming a computer with 2 network cards.

    Monday, October 1, 2012 5:01 PM
  • I had seen the stackoverflow thread and was hoping there was a better way than using Sockets directly.  That article came from 2008 and I was hoping things had been improved by now.

    Is there a way to set "normal IP rooting" to decide which card to use?  How does the software determine what card is "normal"?  If it is part of computer configuration it seems like it goes to the first one installed.  I cannot find any settings for this.

    The article referenced is just about general network routing and does not talk about programming a computer with 2 network cards.

    You can use route to do this. See: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/route.mspx?mfr=true

    (Note that the link I pasted above about the routine configuration gives you other options, as well).


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, October 1, 2012 6:03 PM
    Moderator
  • If the IP addresses and the Mask are set properly on the network cards your software should not have to do anything to determine which card should be used.  It becomes automatic.  Let me give an example if you have two network cards like below

    Card 1 : 192.1.1.1  Mask 0.0.0.0

    Card 2 : 193.1.1.1 Mask 255.0.0.0

    Card 1 is the default gateway computer because it has a mask of 0.0.0.0.  Card 2 is the gateway to subnet 193.X.X.X.  Here are some common rules that should be used when configuring your cards

    1) Only one card should have a mask of 0.0.0.0

    2) The card with the mask of 0.0.0.0 should point to the outside world which is the computer with internet access.

    3) No two cards on the same PC when you AND the IP address and Mask together should have exactly the same results.  You wouldn't have the following tow cards configured in the PC

         Card A : 196.10.1.1  Mask 255.0.0.0       This would result with 196.0.0.0

         Card B : 196.20.1.1  Mask 255.0.0.0       This also would result with 196.0.0.0

    4) The card chosen to send a message is the route that is most restrictive route which is determined by the mask.  A route of a mask 255.255.255.0 will be used before a mask of 255.255.0.0.


    jdweng

    Tuesday, October 2, 2012 12:10 AM
  • Amazing answer but it works.

    After Reed Copsey's suggestion and looking at "route" I found a better word was "adapter" and not network card.  Using this to Google for the answer I found several good places that discussed the problem.  Finding the right search key makes all the difference.  Still, none of the answers where any good.  One lengthy thread even came to the conclusion that TCPClient would not work in a computer with multiple adapters.  You had use Sockets in these kinds of computers.  I guess they were wrong.

    Even knowing the answer now I cannot find any suggestion that you could use the adapter mask to influence TCPClient.  Thanks again Joel Engineer.


    • Edited by MichaelBarb Thursday, October 11, 2012 3:41 PM
    Thursday, October 4, 2012 7:52 PM