How to get the IP address of my machine through a 'C' program

S

sunny

Hai,


Anybody can give me idea how to get the ip address of my machine
through a cprogram with out using the socket programming. Is there any
system call to get the ip address of my machine that will be used in
the c program to display the IP address of my machine.


Regards,
Sunny
 
C

Chris Dollin

sunny said:
Anybody can give me idea how to get the ip address of my machine
through a cprogram with out using the socket programming. Is there any
system call to get the ip address of my machine that will be used in
the c program to display the IP address of my machine.

There's no such function in standard C. For portability I'd guess something
POSIXy, so asking in comp.unix.programmer might help (even if you're
/not/ on a Unix box).
 
R

Richard Tobin

Anybody can give me idea how to get the ip address of my machine
through a cprogram with out using the socket programming. Is there any
system call to get the ip address of my machine that will be used in
the c program to display the IP address of my machine.
[/QUOTE]
There's no such function in standard C.

And since a computer can easily have several IP addresses (or none),
you might want to consider what exactly you want.

-- Richard
 
S

sunny

i need to write a c program to get the ip of my own machine without
using the sockets in it.
 
S

santosh

sunny said:
i need to write a c program to get the ip of my own machine without
using the sockets in it.

Please quote the post you're replying to.

The standrad C language has no facility to do what you want. You'll
probably have to call a POSIX function like gethostname() or
gethostid(). Please look it up in your system's documentation or take
this question to whatever group that is more specific to your
platform. For example, if you're on a UNIX, post to
comp.unix.programmer.
 
R

Richard Tobin

sunny said:
i need to write a c program to get the ip of my own machine without
using the sockets in it.

But why? What should the program do if the machine hasn't got an IP
address, or has several?

And as you've been told, there's no way to do it in standard C; you'll
need to ask in a group that deals with your particular operating
system. There may be a function that returns all the IP addresses
associated with your machine, or perhaps something that tells you the
name (or one of the names) of your machine, and a function that lets
you look up the IP address(es) assocument with that name.

-- Richard
 
C

Christopher Layne

There's no such function in standard C.

And since a computer can easily have several IP addresses (or none),
you might want to consider what exactly you want.

-- Richard[/QUOTE]

And amusingly on top of that, the procedure used to discover the assigned IPs
to each interface on a host is neither POSIX compliant nor standard. About
the most "portable" way is using ioctl's which are system specific and hoping
that the system you're on supports the more "standard" ioctl's.

It pretty much sucks but is documented in UNPv1 (Stevens).
 
K

Kenny McCormack

But why? What should the program do if the machine hasn't got an IP
address, or has several?

And as you've been told, there's no way to do it in standard C; you'll
need to ask in a group that deals with your particular operating
system. There may be a function that returns all the IP addresses
associated with your machine, or perhaps something that tells you the
name (or one of the names) of your machine, and a function that lets
you look up the IP address(es) assocument with that name.

IOW:

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language
 
K

Kenneth Brody

sunny said:
i need to write a c program to get the ip of my own machine without
using the sockets in it.

You can't do this in standard C. The closest you can do is:

char *GetMyIP()
{
return "127.0.0.1";
}

However, I doubt this is sufficient for what you need/want.

There are "standard" function calls, outside the scope of standard C,
which you can use. Perhaps something like gethostid()? However, to
get more details, you need to ask elsewhere. Perhaps this is topical
in comp.unix.programmer?

Of course, as has been pointed out, what happens if the machine has
more than one IP address? What if it has none?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
C

Clever Monkey

Kenneth said:
You can't do this in standard C. The closest you can do is:

char *GetMyIP()
{
return "127.0.0.1";
}

However, I doubt this is sufficient for what you need/want.

I'll add to the library:

char *GetMyIPv6() {
return "Not implemented";
}

Sorry. Couldn't resist.
 
K

Kenny McCormack

Kenneth Brody said:
Of course, as has been pointed out, what happens if the machine has
more than one IP address? What if it has none?

More to the point, "machines" don't have IP addresses. NICs do.
And, in many (most?) cases, the IP assigned to a given NIC isn't
the IP address that you care about anyway (because of NAT).
 
K

Kenneth Brody

Clever said:
I'll add to the library:

char *GetMyIPv6() {
return "Not implemented";
}

Sorry. Couldn't resist.

Shouldn't that be "return NULL;" to indicate an error? :)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
W

Walter Roberson

Kenny McCormack said:
More to the point, "machines" don't have IP addresses. NICs do.

NICs only need to read and write packets; everything else is up to
the IP stack or is an optimization with the cooperation of the IP
stack.
 
G

Gordon Burditt

Anybody can give me idea how to get the ip address of my machine
through a cprogram with out using the socket programming. Is there any
system call to get the ip address of my machine that will be used in
the c program to display the IP address of my machine.

*THE* IP address? Isn't that a little like telling someone to press
*THE* key on *THE* keyboard?

Assuming it's got networking and TCP/IP, "127.0.0.1" is a surprisingly
portable answer to this question.
 
R

Randy Howard

i need to write a c program to get the ip of my own machine without
using the sockets in it.

As others have indicated, and you have snipped away the context, this
is tricky to do, and outside the scope of this particular newsgroup.
You should ask in a newsgroup appropriate to your platform, and keep in
mind that doing this correctly can be tricky, especially with systems
with more than one network adapter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top