Thread: how get ip address on 2 network cards

  1. #1
    Registered User
    Join Date
    Jun 2005

    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

  3. #3
    Registered User
    Join Date
    Jun 2005
    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
    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.

    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2005
    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
    For Windows (using Winsock 2), use WSAIoctl() with SIO_GET_INTERFACE_LIST.

    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:


  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    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
    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
    Registered User
    Join Date
    Jun 2004
    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
    #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);
    		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++){
    			ips[k] = ntohl(ips[k]);
    	}else{//probably was an ip
    	return ips;
    const char* inet_ipttoa(ip_t ip, char*b = NULL){
    	static char ips[22];
    	if(b==NULL) b=ips;
    	return b;
    int main(){
    	ip_t* ips = gethostaddr("");
    	while( *ips )
    	return 0;
    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, 07:17 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. MSN Vital Information
    By iain in forum A Brief History of
    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