AKB Forums

Go Back   AKB Forums > Technical sections > Languages, Compilers and Interpreters
Home Register Blogs FAQ Members List Calendar Downloads Arcade Mark Forums Read

Languages, Compilers and Interpreters C,C++,C#,.NET,Java,PHP,Perl,SQL and more

Troubles when posting message? Click here! :: Проблемы с отправлением сообщения? Нажмите сюда!

Reply
 
LinkBack Thread Tools Display Modes
Old Jan 29, 2002, 05:03   #1
Консервативный Демагог
 
VX's Avatar
 
Join Date: Jan 2002
Location: Кавказская Албания
Posts: 889
Rep Power: 7
Reputation: 10
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?
__________________
Праздник к нам приходит...

|^^^^^^^^^'''^\| ||\__
| ВОДКА-ВОДКА | ||','''|'''''''\_____,_
| _..... _ | ||_ _|'__|_____||.........| |
'(@)'(@)'(@)''''''''''''''''''''''*|(@)""""|(@)*
VX is offline   Reply With Quote Quote selected
Old Jan 29, 2002, 14:53   #2
Administrator
 
acid's Avatar
 
Join Date: Sep 2001
Location: Yerevan, Armenia
Posts: 7,144
Blog Entries: 15
Rep Power: 10
Reputation: 278
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.
__________________
Chat with acid


acid is offline   Reply With Quote Quote selected
Old Jan 29, 2002, 15:48   #3
Banned
 
Yana's Avatar
 
Join Date: Jan 2002
Location: Yerevan
Posts: 1,503
Rep Power: 0
Reputation: 10
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>
Yana is offline   Reply With Quote Quote selected
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 00:28.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
This board was founded on September 29, 2001
Powered by Viper Internet

Affordable Web Hosting | ParevNet

Buy text link