dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: How to connect a port to a specified Networkcard?

  1. #1
    Join Date
    Jun 1999
    Location
    North Germany
    Posts
    306

    How to connect a port to a specified Networkcard?

    Hi

    I have a software that creates active and passive TCP connections. On computers with more than one network adapter I need to bind the port to the specified netword adapter.

    For the passive part I did solve the problem successful with:
    Code:
    		memset((void *)&SrvAddr, 0, sizeof(SrvAddr));
    		SrvAddr.sin_family		= AF_INET;
    		SrvAddr.sin_port		= htons( (WORD) ptChannel->nLocalPort );
    		if(ptChannel->pstrSource == NULL)
    		{
    			SrvAddr.sin_addr.s_addr	= INADDR_ANY;
    		}
    		else
    		{
    			SrvAddr.sin_addr.s_addr	= inet_addr(*(ptChannel->pstrSource));
    		}
    		
    
    		// if bind socket to port failed
    		if( bind( hSocket, (const struct sockaddr *) &SrvAddr, sizeof(SrvAddr)) == SOCKET_ERROR )
    		
    		....
    But I fail to manage the active part. So far I have:
    Code:
     
    	struct sockaddr_in		SrvAddr;		// structure of socket address
    	struct in_addr			InAddrDest;		// structure of in-address
    
    	memset( &SrvAddr, 0, sizeof(SrvAddr) );
    	SrvAddr.sin_family	= AF_INET;
    	SrvAddr.sin_port	= htons( (WORD) ptChannel->nRemotePort );
    	
    	InAddrDest.s_addr	= inet_addr( *ptChannel->pstrDestination );
    	// If address by name
    	if( InAddrDest.s_addr == INADDR_NONE )
    	{
    		return ceTcpErrAddrByNameNotSupported;
    	}  // END OF If address by name
    
    	// Copy the IP address
    	SrvAddr.sin_addr.s_addr	= InAddrDest.s_addr;
    
    	// If connect failed
    	if( ( connect( ptChannel->hSocket, (const struct sockaddr *) &SrvAddr, sizeof(SrvAddr) ) ) == SOCKET_ERROR )
    	
    	.....
    Where do I have to add the part where I specify the local network adapter that should be used?!

    I tried bind but then the second bind from the second connection which is passiv fails then.

    BTW my program implements the RFC1006 protocoll which means that the local and remote port is always 102. Can I still connect to different networkcards when I have more than one connection?

    thanks in advance
    Grit
    Last edited by Grit; April 17th, 2008 at 04:34 PM.

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

    Re: How to connect a port to a specified Networkcard?

    Which operating system? Windows? On WinXP and before, it's the stack that selects the network card that is used, and there is nothing in the Winsock API that can be used to affect the stack's selection. A call to bind() will not do anything.

    The stack makes its selection based on something called the "routing table". It might be possible to change the routing table, and a change to the routing table would probably affect the way the stack selects one adapter over another.

    See this thread: http://www.codeguru.com/forum/showth...05#post1698305

    On Windows Vista, there has been a change to the stack, and a call to bind() might make a difference.

    Mike

    PS: I am not certain what you mean by passive versus active TCP connections. In TCP, connected peers are exactly that: peers. One is not active and the other somehow passive. It's typical for one peer to be responsible for initiating the connection through a call to connect(), and if that's what you mean, then fine. There is a also concept of active versus passive closings of TCP connections, but I don't think that you are referring to the closing of a connection.

  3. #3
    Join Date
    Jun 1999
    Location
    North Germany
    Posts
    306

    Re: How to connect a port to a specified Networkcard?

    Yes I mean with active the part with the connect. The problem in my project is that the destination IP Adress is in another network with another network address so that the stack can't know which card is the right one. For example:
    netmask: 255.255.255.0

    card1 has IP 131.100.0.100
    card2 has IP 130.150.0.100

    destination IP: 150.200.10.51

    and only card 2 is connected to the destination network through a router

    I know for UDP you can tell him which card he shall use and now I need the same for the normal TCP

    BTW I use XP and 2000

    Grit

  4. #4
    Join Date
    Aug 2007
    Location
    Birmingham, UK
    Posts
    360

    Re: How to connect a port to a specified Networkcard?

    Setup your router's IP address as the gateway address for the TCP/IP configuration of card 2. Do not setup a gateway for card 1!

    This setup then tells the OS that any traffic for IP addresses not on either of the local networks should go to the gateway, and only card 2 has one. The traffic will therefore be sent out to the gateway (your router) on card 2. From there on your router should be able to route the traffic properly.

  5. #5
    Join Date
    Jun 1999
    Location
    North Germany
    Posts
    306

    Re: How to connect a port to a specified Networkcard?

    that's not possible because on the other card I must reach other computers with the same IP Problem.

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

    Re: How to connect a port to a specified Networkcard?

    I know for UDP you can tell him which card he shall use and now I need the same for the normal TCP
    Even for UDP, I don't think you can specify the card to use. Although it might have appeared to you that it worked, it was probably just a coincidence.

    Did you investigate the CreateIpForwardEntry() function? A quick search turned up this link, which describes how to create a routing table entry so that all IP traffic with a specified server is routed via a specified adapter: http://ce4all.blogspot.com/2007/05/r...specified.html . It describes an approach for a WinCE device, but it should work for desktop too.

    Mike

  7. #7
    Join Date
    Aug 2007
    Location
    Birmingham, UK
    Posts
    360

    Re: How to connect a port to a specified Networkcard?

    You can also use the "route" command in a command prompt window to add to your routing table. The following command
    Code:
    route ADD 150.200.10.51 MASK 255.255.255.0 <gateway> IF <interface> -p
    adds a route for IP 150.200.10.51 with netmask 255.255.255.0 via <gateway> through <interface>. For <gateway> you need to specify the IP address of your gateway and for <interface> the number of the interface you want to route through. You can find out which interface has which number whit the following command
    Code:
    route print
    The option "-p" should make the setting persistent through boots.

  8. #8
    Join Date
    Jun 1999
    Location
    North Germany
    Posts
    306

    Re: How to connect a port to a specified Networkcard?

    Well in UDP I could use the bind method to tell him which card shall be connected to which multicast group address. Why isn't that possible with TCP? We have a real big network with plenty of such entries that we would have to make in the routing table so that my boss wants that my software should be told which card it has to use for a specified address.

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

    Re: How to connect a port to a specified Networkcard?

    Both the "why" and the "how" have been explained above. "Why" can't TCP specify the card? It's an issue with the OS: the Windows OSs before Vista used a "Weak End System" model, which does not let you. "How" can you set it? By setting the routing table.

    Incidentally, Vista uses strong E/S. So maybe the answer to your boss is to switch to Vista.
    Last edited by MikeAThon; April 21st, 2008 at 10:44 AM.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)