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

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 ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.