LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-14-2006, 12:17 PM   #1
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Rep: Reputation: 30
get ip address of the machine that my programming is running


hi,
I was wondering how to I get the IP address of the local machine that my application is running. I've tried numerous things with no success.
Code:
        struct sockaddr_in server, from;

        int sock;

        sock = socket(AF_INET, SOCK_DGRAM, 0);

        bzero(&server, sizeof(server));

        server.sin_family = AF_INET;
        server.sin_addr.s_addr = INADDR_ANY;
        server.sin_port = htons(6969);

        printf("IP:%s\n", inet_ntoa(server.sin_addr));
All this does is print 0.0.0.0 I read online that INADDR_ANY points to IP address of the machine is their a way to use this variable directly?

thanks,

djgerbavor3
 
Old 04-14-2006, 12:29 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

The easiest way is to call gethostbyname () on your own hostname:
http://shoe.bocks.com/net/

Another way is to call getsockname () on any active connection:
http://faq.cprogramming.com/cgi-bin/...&id=1045780608

You can find your hostname with "gethostname()" (man 2, Linux) or "GetComputerName ()" (Windows)

'Hope that helps .. PSM

Last edited by paulsm4; 04-14-2006 at 12:32 PM.
 
Old 04-14-2006, 12:33 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The INADDR_ANY constant is not the IP address of your machine but a "generic every" IP address and is typically used when creating a listening socket so say I'm listening on any IP address, hence the value of 0.0.0.0

As Paul said use the getHostByName()

graeme
 
Old 04-14-2006, 08:57 PM   #4
djgerbavore
Member
 
Registered: Jun 2004
Location: PA
Distribution: Fedora (latest git kernel)
Posts: 458

Original Poster
Rep: Reputation: 30
Hey guys thanks for the help, but unfortunately, that didn't solve my problem. First gethostbyname, requires me to have a nameserver running or something to translate hostname to ip. which i have none, i have a computer name, but when i use that name i just get 127.0.0.1 which is no good. Secondly, getsockname() doesnt' work, because I'm using UDP, and it requires the socket to be in 'connected' mode which UDP isn't. yet is their still another way I can do this?


thanks,

djgerbavor3
 
Old 04-14-2006, 09:52 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
To the best of my knowledge, "gethostbyname()" works under all circumstances - via your local /etc/hosts (if that's all you have), or DNS (if available).

I also believe you can configure the behavior with /etc/nsswitch.conf.

Did you try writing a standalone program that did gethostname(), followed by "gethostbyname ()"? What happened?

Plan C might be to do a "popen()" and parse the output from ifconfig, or look in /proc.

PS:
There's also a thread on this very LQ page that might help with your quest, too:
http://www.linuxquestions.org/questi...d.php?t=434241

Last edited by paulsm4; 04-15-2006 at 12:18 AM.
 
Old 04-15-2006, 01:05 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
See that one too:
http://www.linuxquestions.org/questi...d.php?t=425637
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find an IP address from the MAC address of a remote machine ? jitz Linux - General 3 01-03-2006 07:55 AM
How to find IP address of a machine if I know their MAC Address dysenteryduke Linux - Networking 13 09-12-2005 10:21 AM
how to get ip address, broadcast address, mac address of a machine sumeshstar Programming 2 03-12-2005 04:33 AM
How to know MAC address ( Machine address)?? vanhelsing Linux - Networking 3 08-12-2004 09:30 AM
find MAC ADDRESS programming C ofada Linux - Hardware 1 01-15-2004 07:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration