Thread: Finding IP adress

  1. #1
    Registered User
    Join Date
    Feb 2005

    Finding IP adress

    I'm a linux user and would like to create a basic program that outputs the local and internet IP that is currently in use by the machine. Does anyone know where I might obtain these 2 things from the system?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    When you say local do you mean, because it will always be that. Or do you mean you have 2 NICs in the box and 1 connects to the Internet and another one connects to a LAN?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2005
    I should have been a bit more specific. What I am after is the IP you are assigned by a central server. My setup is that computer 1 assigns me an IP. I want to find that IP and the current IP given to me by my internet service provider.

  4. #4
    Join Date
    Nov 2003
    The /etc/hosts file - the first valid entry is the node name and node IP address of the current box the process is running on.

    See also gethostbyaddr() or gethostent() in the man pages.

    uname() also shows the node name in the utsname struct.

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    This example doesn't work but good luck fixing it:

    #include <stdio.h>
    #include <stdlib.h> 
    #include <al/etl.h>
    #include <sfl.h>
    #include <sys/types.h> 
    #include <sys/socket.h> 
    #include <netinet/in.h> 
    #include <netdb.h> 
    #define EXIT_CRAP 1
    #define SOME_PORT_I_GUESS 80
    void ........( const char *msg )
    	printf( "........: %s", msg );
    	exit( EXIT_CRAP );
    void makeReceiverSA(struct sockaddr_in *sa, int port)
    	sa->sin_family = AF_INET;
    	sa->sin_port = htons( port );
    	sa-> sin_addr.s_addr = htonl( INADDR_ANY );
    int main( void )
    	struct sockaddr_in STUPID_SOCKET;
    	int s;
    	if( ( s = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 ) 
    		........( "Stupid ass sockets" );
    	printf( "sa = %d, %s, %d\n", STUPID_SOCKET.sin_family, 
    		inet_ntoa( STUPID_SOCKET.sin_addr ), 
    		ntohs( STUPID_SOCKET.sin_port ) );
    	if( bind( s, ( struct sockaddr * )&STUPID_SOCKET, sizeof( struct sockaddr_in ) ) != 0 )
    		close( s );
    		........( "WTF is bind!?\n" );
    	close( s );
    	return EXIT_SUCCESS;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. How to read IP Adress!
    By Smoki in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2005, 06:56 AM
  3. IP adress in Linux?
    By IM! in forum Linux Programming
    Replies: 7
    Last Post: 02-27-2005, 04:56 PM
  4. finding out ip address?
    By chiatello in forum Linux Programming
    Replies: 3
    Last Post: 08-07-2003, 06:18 AM