Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters

Reply
 
Thread Tools

gethostbyname() problem
Old 29.01.2002, 06:03   #1
VX
Консервативн
 
VX's Avatar
 
Join Date: 01 2002
Location: Кавказская Албания
Posts: 889
Rep Power: 0
Red face 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?
__________________
Праздник к нам приходит...

|^^^^^^^^^'''^\| ||\__
| ВОДКА-ВОДКА | ||','''|'''''''\_____,_
| _..... _ | ||_ _|'__|_____||.........| |
'(@)'(@)'(@)''''''''''''''''''''''*|(@)""""|(@)*

Old 29.01.2002, 15:53   #2
Moderator
 
acid's Avatar
 
Join Date: 09 2001
Location: South Korea, Gumi
Posts: 7,699
Blog Entries: 16
Rep Power: 7
Post

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.

Old 29.01.2002, 16:48   #3
Banned
 
Yana's Avatar
 
Join Date: 01 2002
Location: Yerevan
Posts: 1,503
Rep Power: 0
Post

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>
Reply




Реклама:
реклама
Buy text link .

All times are GMT. The time now is 06:44.
Top

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