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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to set a socket's Ethernet adapter?

How to set a socket's Ethernet adapter?

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmintutorialquestion
3 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.
  • M Offline
    M Offline
    masnu
    wrote on last edited by
    #1

    Is there any way to specify which Ethernet adapter a socket connetion uses? I'm creating the socket as follows: SOCKET Socket; int opt_on = 1; // Create socket Socket = socket(AF_INET, SOCK_STREAM, 0); if(Socket == INVALID_SOCKET) return - 1; // Set socket reuse option if( setsockopt( Socket, SOL_SOCKET, SO_REUSEADDR, (char*)&opt_on, sizeof (opt_on) ) == SOCKET_ERROR ) { return -1; } //__if( setsockopt(...) == SOCKET_ERROR )__ // Set client properties cliAddr.sin_family = AF_INET; cliAddr.sin_addr.s_addr = inet_addr( "169.254.148.9" ); cliAddr.sin_port = htons( 4200 ); // Connect to server if ( connect( Socket, (struct sockaddr *)&cliAddr, sizeof(cliAddr) ) == SOCKET_ERROR ) { GetSocketError(); return -1; } //__if ( connect(...) == SOCKET_ERROR )__ The connection will always try to use the first available network adapter, which is not necessarily the one connected to the requested port. Is there a way to change this without having to specify different domains? Thanks.

    M C 2 Replies Last reply
    0
    • M masnu

      Is there any way to specify which Ethernet adapter a socket connetion uses? I'm creating the socket as follows: SOCKET Socket; int opt_on = 1; // Create socket Socket = socket(AF_INET, SOCK_STREAM, 0); if(Socket == INVALID_SOCKET) return - 1; // Set socket reuse option if( setsockopt( Socket, SOL_SOCKET, SO_REUSEADDR, (char*)&opt_on, sizeof (opt_on) ) == SOCKET_ERROR ) { return -1; } //__if( setsockopt(...) == SOCKET_ERROR )__ // Set client properties cliAddr.sin_family = AF_INET; cliAddr.sin_addr.s_addr = inet_addr( "169.254.148.9" ); cliAddr.sin_port = htons( 4200 ); // Connect to server if ( connect( Socket, (struct sockaddr *)&cliAddr, sizeof(cliAddr) ) == SOCKET_ERROR ) { GetSocketError(); return -1; } //__if ( connect(...) == SOCKET_ERROR )__ The connection will always try to use the first available network adapter, which is not necessarily the one connected to the requested port. Is there a way to change this without having to specify different domains? Thanks.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      masnu wrote:

      Is there any way to specify which Ethernet adapter a socket connetion uses?

      For a local socket, that's what the bind() function is for.

      masnu wrote:

      The connection will always try to use the first available network adapter, which is not necessarily the one connected to the requested port.

      I'm confused here. The port you specified is for the destination address, not the local address. Again, use bind() to bind a socket to the adapter with the specific address before calling connect(). Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      1 Reply Last reply
      0
      • M masnu

        Is there any way to specify which Ethernet adapter a socket connetion uses? I'm creating the socket as follows: SOCKET Socket; int opt_on = 1; // Create socket Socket = socket(AF_INET, SOCK_STREAM, 0); if(Socket == INVALID_SOCKET) return - 1; // Set socket reuse option if( setsockopt( Socket, SOL_SOCKET, SO_REUSEADDR, (char*)&opt_on, sizeof (opt_on) ) == SOCKET_ERROR ) { return -1; } //__if( setsockopt(...) == SOCKET_ERROR )__ // Set client properties cliAddr.sin_family = AF_INET; cliAddr.sin_addr.s_addr = inet_addr( "169.254.148.9" ); cliAddr.sin_port = htons( 4200 ); // Connect to server if ( connect( Socket, (struct sockaddr *)&cliAddr, sizeof(cliAddr) ) == SOCKET_ERROR ) { GetSocketError(); return -1; } //__if ( connect(...) == SOCKET_ERROR )__ The connection will always try to use the first available network adapter, which is not necessarily the one connected to the requested port. Is there a way to change this without having to specify different domains? Thanks.

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

        Been a while, but if i recall correctly, - use GetAdaptersInfo to walk through the adapters - for each adapter you can walk its list of (local) IP addresses - create the socket then bind to the local address for the adapter you want - after the bind connect to the desired peer

        ...cmk The idea that I can be presented with a problem, set out to logically solve it with the tools at hand, and wind up with a program that could not be legally used because someone else followed the same logical steps some years ago and filed for a patent on it is horrifying. - John Carmack

        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