hostent problem

G

games60

Hi,
I want to get the IP address of my machine in my program and for that i
have written the following code.
I don't know why the gethostbyname() is not returning the correct
hostent structure.

hp = new hostent[];
TCHAR str[MAX_COMPUTERNAME_LENGTH+1];
TCHAR result[MAX_COMPUTERNAME_LENGTH+1];

GetComputerName(str,&len);
hp = gethostbyname(str);
if(hp!=NULL)
{
memcpy(result, hp->h_name, sizeof(result));
OR
result=(TCHAR*)hp->h_name;
}

The result that i am getting the hp=NULL.

Thanks,
Saurabh Aggrawal
 
R

Robert Bunn

Hi,
I want to get the IP address of my machine in my program and for that i
have written the following code.
I don't know why the gethostbyname() is not returning the correct
hostent structure.

hp = new hostent[];

I think this shouldn't compile, and MinGW agrees. You can't omit the
size of an array allocated using new. You don't want to allocate it
anyway. You just want a pointer:

hostent* hp;
TCHAR str[MAX_COMPUTERNAME_LENGTH+1];
TCHAR result[MAX_COMPUTERNAME_LENGTH+1];

GetComputerName(str,&len);
hp = gethostbyname(str);
if(hp!=NULL)
{
memcpy(result, hp->h_name, sizeof(result));
OR
result=(TCHAR*)hp->h_name;
}

The result that i am getting the hp=NULL.
 
G

games60

Hi,

Now the code is:

hostent* hp;
TCHAR result[MAX_COMPUTERNAME_LENGTH­+1];

hp = gethostbyname("localhost");
if(hp!=NULL)
{
result=(TCHAR*)hp->h_name;
}

I am still getting the same output. hp=NULL.
 

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

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top