Armenian Knowledge Base

Armenian Knowledge Base (https://forum.armkb.com/)
-   Languages, Compilers, Interpreters (https://forum.armkb.com/languages-compilers-interpreters/)
-   -   gethostbyname() problem (https://forum.armkb.com/languages-compilers-interpreters/45-gethostbyname-problem.html)

VX 29.01.2002 06:03

gethostbyname() problem
 
Hi there.

problema v chem.. <BR><BLOCKQUOTE><font size="1" face="MS Sans Serif, Verdana, Helvetica, sans-serif">code:</font><HR><pre><BR> struct hostent *hp;<BR> long ip;<BR> if((hp=gethostbyname(name))==NULL)<BR> {<BR> fprintf(stderr,"Can't resolve host.\n");<BR> exit(0);<BR> }<BR> memcpy(&ip,(hp->h_addr),4);<BR> return ip;<BR></pre><HR></BLOCKQUOTE>

ne pochemuto ne rabotaet.<BR>Who can help me?

acid 29.01.2002 15:53

Hi.<BR>If that's all you wrote then you forgot WSAStartup which in fact is loading winsock dynamic libraries into your process memory and initializing it so your application can be prepared to call gethostbyname().

The code is following:<BR><BLOCKQUOTE><font size="1" face="MS Sans Serif, Verdana, Helvetica, sans-serif">code:</font><HR><pre><BR> WSADATA wsaData;<BR> if(WSAStartup(0x0101, &wsaData))<BR> {<BR> printf("WSAStartup failed %s\n", WSAGetLastError());<BR> }

// Do your stuff here

</pre><HR></BLOCKQUOTE>

...and don't forget to call WSACleanup() once you've done with sockets.

Yana 29.01.2002 16:48

Es ays code ogtagorcel em vor stanam IP- address in standard notation (dotted)...<BR><BLOCKQUOTE><font size="1" face="MS Sans Serif, Verdana, Helvetica, sans-serif">code:</font><HR><pre>

<BR> char name[100];<BR> struct hostent *hp;<BR> long ip;

WORD wVersionRequested; <BR>WSADATA wsaData; <BR>int err=0; <BR>int wsaerr=0;

wVersionRequested = MAKEWORD(1, 1); <BR>err = WSAStartup(wVersionRequested,&wsaData); <BR>wsaerr=WSAGetLastError(); <BR> <BR>if (err != 0) <BR> return 123; <BR> <BR>if ( LOBYTE( wsaData.wVersion )!= 1||HIBYTE( wsaData.wVersion )!= 1) <BR>{<BR> WSACleanup(); <BR> return 1234; <BR>}

<BR>if ( LOBYTE( wVersionRequested ) < 1 || <BR> ( LOBYTE( wVersionRequested ) == 1 && <BR> HIBYTE( wVersionRequested ) < 1 ) <BR> )<BR> return WSAVERNOTSUPPORTED;

gethostname( name, sizeof( name ));

if((hp=gethostbyname(name))==NULL)<BR> {<BR> fprintf(stderr,"Can't resolve host.\n");<BR> exit(0);<BR> }

memcpy(&ip,&*(*((*hp).h_addr_list)),4);<BR>//or:<BR> memcpy(&ip,(hp->h_addr),4);

//The macro h_addr is defined to be //h_addr_list[0] for compatibility with older //software. <BR> <BR> return ip;<BR> </pre><HR></BLOCKQUOTE>


All times are GMT. The time now is 18:24.

Powered by vBulletin® Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.