Click to See Complete Forum and Search --> : I want print my IP


majidpics
12-22-2002, 10:46 AM
I am in linux using gcc
I want to print my ip address

I am doing this

my_addr.sin_family=AF_INET;
my_addr.sin_port=1234;
my_addr.sin_addr.s_addr=htonl(INADDR_ANY);

but when i print the address like
printf("%s",inet_ntoa(my_addr.sin_addr));

it prints

0.0.0.0

what should i do.

It will be better if you reply with example. Thankx

mnsharif
12-22-2002, 11:56 PM
well, it must be 0.0.0.0
coz u r specifying INADDR_ANY which means, that i can accept from any IP address. I mean it was not supposed to give you your local IP address,

there must be some way to get it, but i dont know, hope anyone else here, will help you out,:D :D

majidpics
12-23-2002, 01:31 AM
Well i think, you are right but not almost

bcz

I send a buffer using sendto from my machine.
On other machine I recieved the buffer and checked the sender's
address.
It was mine machine's address.

thats why ...

Stuka
12-23-2002, 11:52 AM
Look at gethostbyname() - it returns a hostent structure that contains all of the local machine's addresses. INADDR_ANY is indeed all 0's, because it's a special address that the TCP/IP stack fills in with one of the IP addresses on your machine.

majidpics
12-23-2002, 12:58 PM
Thanks. I have done with it.