CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2009
    Posts
    2

    how to open sockets on multiple network adapters..??

    Hi

    I have two different network adapters on my PC
    1- 3G EVDO USB DEVICE
    2- USB WIFI STICK (TP-LINK)
    both adapters have internet connection.

    Note: this is for client side application

    when i open a socket on one adapter when the other is disconnected both of them works fine.
    but when both are connected and i open sockets on both of them only the 3G EVDO device socket works.
    I have binded both the sockets to their respective adapters local IP. and both of them are connecting to the same remote ip (where server application is running) but on different ports.

    please tell me that is there any way that both the sockets can work simultaneously..??
    please help me..


    Thanks and regards
    Hussain Aftab

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: how to open sockets on multiple network adapters..??

    Binding each socket to its respective adapters should be sufficient. You say one doesn't work. What doesn't work? connect? send/recv? Are there any errors returned and what are their codes?

    What does your bind code look like?

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: how to open sockets on multiple network adapters..??

    Quote Originally Posted by hoxsiew View Post
    Binding each socket to its respective adapters should be sufficient.
    I tend to disagree with this, at least with respect to outgoing (i.e., client) connections. For a multi-homed system (i.e., a system with multiple network cards) with a Windows OS, and with respect to outgoing connections, a call to bind() will have no effect on the choice of which card will be used to establish the connection. Rather, the choice of card will be made by the OS using the routing table. The OS will make its decision by comparing the routing table to the destination IP, to choose the card that seems most appropriate for the destination.

    See this blog for an example on how to create an entry in the routing table. It's written for WinCE but the principles are similar: "Routing IP traffic via specified adapter" at
    http://ce4all.blogspot.com/2007/05/r...specified.html

    OTOH, for accepting incoming connections (i.e., via listen() and accept()), a call to bind() works as expected.

    Mike

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    Re: how to open sockets on multiple network adapters..??

    Yes, I see. I was incomplete in my description. The bind must use the adapter address:

    Code:
        struct sockaddr_in my_addr;
    
        my_addr.sin_family=AF_INET;
        my_addr.sin_port=0;
        my_addr.sin_addr.s_addr=inet_addr("192.168.1.100");
        bind(sock,(struct sockaddr *)&my_addr,sizeof(my_addr));
        //connect, send,recv,etc.

  5. #5
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: how to open sockets on multiple network adapters..??

    Quote Originally Posted by hoxsiew View Post
    Yes, I see. I was incomplete in my description. The bind must use the adapter address:
    Even with the address of the adapter, my point was that for an outgoing connection, the call to bind() is ignored and will not effectuate a selection of the specified adapter. In Windows, it's the OS that chooses the adapter for outgoing connections, and it does so at the time of the call to connect(), based on a comparison of the IP address of the destination to the information in the routing table.

    The address of the destination does not come into play until the call to connect(), such that any prior call to bind() is effectively ignored for outgoing connections on a multihomed system.

  6. #6
    Join Date
    Feb 2005
    Posts
    2,160

    Re: how to open sockets on multiple network adapters..??

    I must be having a senior moment or something. I'm confusing bind/listen/accept on the server server side of things. Please ignore anything I've said so far.

  7. #7
    Join Date
    Sep 2009
    Posts
    2

    Re: how to open sockets on multiple network adapters..??

    I agree with MikeAThon.
    It seems that binding a socket to a network adapters ip doesn't do the trick. because if you see the routing table of windows it shows the Default gateway to the EVDO device and therefore all the packets are being routed towards that adapter..
    we need to change the default gateway or something in the routing table as the article on the given link said.
    http://ce4all.blogspot.com/2007/05/r...specified.html
    but i didn't understand how to use those functions and what i have to include in my code to get access to those functions.?

  8. #8
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: how to open sockets on multiple network adapters..??

    Before trying to accomplish this programmatically, you might want to confirm that it works if you add an entry to the routing table manually.

    See the "Route Add" function: "Route" at http://www.microsoft.com/resources/d....mspx?mfr=true

    Add the entry manually, and then see if your program works as expected, i.e., confirm that one card is selected for one destination IP address, and the other card is selected for a second destination IP address.

  9. #9
    Join Date
    Jun 2002
    Location
    Bozeman, MT
    Posts
    8

    Re: how to open sockets on multiple network adapters..??

    I'm not so sure that binding won't work. There are a couple of guys on stackoverflow ( http://stackoverflow.com/questions/4...ng-to-a-socket ) that are saying that calling bind() before connect() will force a socket to use a specified NIC.

    j_random_hacker said:

    So how can a client choose what NIC to use? A client can also choose to call bind() if it so desires, after socket() and before connect(). Usually this isn't done simply because connect() will automatically bind an unbound socket in a way that enables access via any NIC (which is usually the desired behaviour), but this auto-binding can be turned off by calling bind() explicitly. In this case, you should specify 0 for the port number to have the OS choose a random port number for you.

  10. #10
    Join Date
    Jun 2002
    Location
    Bozeman, MT
    Posts
    8

    Re: how to open sockets on multiple network adapters..??

    I found more support for the bind() before connect() theory here: http://stackoverflow.com/questions/4...troller-to-use and here: http://lists.apple.com/archives/macn.../msg00084.html

    The documentation for winsock says "If the socket is unbound, unique values are assigned to the local association by the system, and the socket is marked as bound." I suppose one might read that as "if connect() causes unbound sockets to auto-bind, then pre-bound sockets will get to use whatever association they were bound with". But really, the wording is ambiguous so i don't know if it's safe to make that conclusion based on the winsock documentation.

    It seems that Windows XP and Windows & handle a bind() before connect() slightly differently: http://www.codeproject.com/Messages/...e-connect.aspx

    Another theory I ran across is using setsockopt() with SO_BINDTODEVICE. See: http://stackoverflow.com/questions/4...at-client-code . There is a really good explanation as to why bind() before connect() isn't as good as SO_BINDTODEVICE here: http://codingrelic.geekhold.com/2009...dtodevice.html . So perhaps MikeAThon is correct....

  11. #11
    Join Date
    May 2013
    Posts
    12

    Re: how to open sockets on multiple network adapters..??

    how to use wishark in network course

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: how to open sockets on multiple network adapters..??

    Quote Originally Posted by zeeshanaayan07 View Post
    how to use wishark in network course
    What does this question have to do with the OP which is "how to open sockets on multiple network adapters..?"?
    Please, remove this post here and create a new thread with the complete problem description!
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured