how get ip address on 2 network cards

This is a discussion on how get ip address on 2 network cards within the C Programming forums, part of the General Programming Boards category; hi, My machine has 2 network cards, and I use network card which is not default card connect to server. ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    4

    Unhappy how get ip address on 2 network cards

    hi, My machine has 2 network cards, and I use network card which is not default card connect to server. I wanna get ip address from this network card, but gethostbyname() give me only default ip address card which I don't want. What function should I use instead? or you guy has an brilliant idea with this function? Thank you in advance.
    Last edited by akesayyo; 06-30-2005 at 04:51 AM. Reason: grammar error

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Yet another post which neglects any useful information, like which OS and compiler is being used.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    I am so sorry for this topic, but I am actually a newbie. I am not good at socket programmimg, and I tried to find relate topic, but didn't find it. So what is the solution, or you can post the related topic, thank you.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Apparently you can't read either. Let's refresh your memory:
    Quote Originally Posted by Salem
    Yet another post which neglects any useful information, like which OS and compiler is being used.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    SunOS 5.8
    gcc version 2.95.3
    -----------------------
    Due to my project is an api, which is used for Linux, Sun, Windows, AIX , so I can't list you all information about OS and compiler. Sorry for neglect posting .
    Last edited by akesayyo; 07-04-2005 at 12:34 AM. Reason: add new information

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    For Windows (using Winsock 2), use WSAIoctl() with SIO_GET_INTERFACE_LIST.
    http://support.microsoft.com/kb/186312/EN-US/

    For *nix, use ioctl() with SIOCGIFCONF. Using this in a system independent manner is a mess. SVR4-based systems do it differently from BSD-based systems. You'll also find differences between Sun-OS versions themselves. There are several different code examples in comp.protocols.tcp-ip:
    http://groups-beta.google.com/group/...&q=SIOCGIFCONF

    gg

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    ACE is a multi-platform communications library. ACE::get_ip_interfaces includes code to get interface addresses on more platforms than you could poke a stick at.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    4
    Thank you all of you,

    I have another question to ask. I found bug with my api when I use my second network card to connect to server. how can I get actual ip of connecting network card (the second one) ? Because I used gethostbyname function and it gives me only primary ip of network card. Because my api project is a low level programming that mean it uses a basic function such a gethostbyname, gethostname, getsockname, etc. Which one of these functions can solve the problem? Thank you in advance.

  9. #9
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    the regular socket API (socket, accept, connect, send, recv, listen, bind, ...) is supported both by posix and MS compilers, so it's safe to assume portability to a large number of systems.

    gethostbyname() returns a hostent struct pointer. Examine field h_addr_list
    http://www.opengroup.org/onlinepubs/...s/netdb.h.html
    Code:
    #include <iostream>
    #include <cstring>
    
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <sys/socket.h>
    
    typedef unsigned long ip_t;
    
    ip_t* gethostaddr(const char *hn,ip_t* ips = NULL,uint sz = 0){
    	static const unsigned int IPBSSZ = 1024;
    	static ip_t ipbs[IPBSSZ];
    	if(ips==NULL){ ips=ipbs;sz=IPBSSZ; }
    	else if(sz==0) return NULL;
    	else if(sz==1){ *ips=0;return ips; }
    
    	struct hostent *hostinfo = gethostbyname(hn);
    	if(hostinfo!=NULL){
    		uint k, szip = ((signed)sizeof(ip_t))>hostinfo->h_length?hostinfo->h_length:sizeof(ip_t);
    		for(k=0;hostinfo->h_addr_list[k] && k+1<sz && k<(IPBSSZ-1);k++){
    			memcpy(ips+k,hostinfo->h_addr_list[k],szip);
    			ips[k] = ntohl(ips[k]);
    		}
    		ips[k]=0;
    	}else{//probably was an ip
    		ips[0]=inet_addr(hn);
    		ips[1]=0;
    	}
    	return ips;
    }
    
    const char* inet_ipttoa(ip_t ip, char*b = NULL){
    	static char ips[22];
    	if(b==NULL) b=ips;
    	sprintf(b,"%u.%u.%u.%u",(uint)((ip>>24)&0xff),(uint)((ip>>16)&0xff),(uint)((ip>>8)&0xff),(uint)((ip>>0)&0xff));
    	return b;
    }
    
    
    int main(){
    	ip_t* ips = gethostaddr("www.google.com");
    	
    	while( *ips )
    		std::cout<<inet_ipttoa(*ips++)<<std::endl;
    	
    	return 0;
    }
    output
    Code:
    66.249.87.104
    66.249.87.99
    to get the ips of your computer, use it's network name.
    Last edited by xErath; 07-05-2005 at 02:20 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. MSN Vital Information
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 09-22-2001, 08:55 PM
  4. how to listen to multiple Network cards in one go?
    By C beginner in forum C++ Programming
    Replies: 0
    Last Post: 08-16-2001, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21