[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

hostent problem

Saurabh Aggrawal

6/30/2005 11:36:00 AM

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

2 Answers

Robert Bunn

6/30/2005 12:38:00 PM

0


<games60@hotmail.com> wrote in message
news:1120131331.929300.236040@f14g2000cwb.googlegroups.com...
> 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.


Saurabh Aggrawal

7/1/2005 9:29:00 AM

0

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.