0

HI All, i know its a known issue but i am again back with same problem.I am trying to find IP of the host. USed this code.:

struct sockaddr_in sock_name;
int sock_size = sizeof(sock_name);
struct in_addr remote_addr;
int sockfd, size = 15;

if (0 > (sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP)))
 {
        return;
 }

getsockname(sockfd, (struct sockaddr *)&sock_name, (socklen_t*)&sock_size);
remote_addr = sock_name.sin_addr;
sprintf(tempbuff,"%s",inet_ntoa(remote_addr));

DONT GO FOR MISTAKES ITS JUST A PART OF CODE FOR FINDING IP.
Now the problem is that the ip it returns is 0.0.0.0

and i think i have done the right thing opening a socket and then taking the ip to which that socket is associated.


Please dont suggest gethostbyname function. Cant use that function as its not supported in my JVM.


Is there any other way in c to find the ip....or can anybody tell me what i am doing wrong in the code....
thanks for the help
regards
habatra

Edited by Nick Evan: added code-tags

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by CrazyDieter
0

Please dont suggest gethostbyname function. Cant use that function as its not supported in my JVM.

gethostbbyname is available on every decent platforms, I don't see why you wouldn't use it !


Actually, I don't understand the purpose of your code : you do not provide any information about the remote adresse you want to reach , your socket is not bound to any host when you call getsockname...

Please explain you problem with more details : what do you really want to do ?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.