How to set a socket's Ethernet adapter?
-
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. -
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.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:
-
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.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