Armenian Knowledge Base  

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

Reply
 
LinkBack Thread Tools
Old 29.01.2002, 06:03   #1
Консервативн
 
VX's Avatar
 
Join Date: 01 2002
Location: Кавказская Албания
Posts: 889
Downloads: 0
Uploads: 0
Reputation: 0 | 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?
__________________
Праздник к нам приходит...

|^^^^^^^^^'''^\| ||\__
| ВОДКА-ВОДКА | ||','''|'''''''\_____,_
| _..... _ | ||_ _|'__|_____||.........| |
'(@)'(@)'(@)''''''''''''''''''''''*|(@)""""|(@)*
Reply With Quote
Old 29.01.2002, 15:53   #2
Moderator
 
acid's Avatar
 
Join Date: 09 2001
Location: South Korea, Gumi
Posts: 7,699
Downloads: 102
Uploads: 34
Blog Entries: 16
Reputation: 561 | 6
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.
Reply With Quote
Old 29.01.2002, 16:48   #3
Banned
 
Yana's Avatar
 
Join Date: 01 2002
Location: Yerevan
Posts: 1,503
Downloads: 0
Uploads: 0
Reputation: 0 | 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 With Quote
Sponsored Links
Reply

Thread Tools


На правах рекламы:
реклама

All times are GMT. The time now is 17:44.


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