Internet Development

Moderators: None (Apply to moderate this forum)
Number of threads: 1020
Number of posts: 1885

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Edit Report
Winsock: GetHostByAddr Posted by Phreedom on 7 Aug 1999 at 2:23 PM
I'm writing a simple program to convert an IP address to a hostname, but I'm having trouble with the function GetHostByAddr (found in winsock.h (windows) or netdb.h (linux/unix)). The function takes three arguments (const char FAR * addr,int len, int type) len=4 and type=AF_INET. I've tried simply passing a string (ie: "127.0.0.1") as the addr argument, but it doesn't work. I think it wants the address in network byte order but I can't figure it out. If you have an idea what the problem is (or source code, or a suggestion) please tell me. I will post my source in my next message.


Edit Report
Re: Winsock: GetHostByAddr (source code) Posted by Phreedom on 7 Aug 1999 at 2:36 PM
<p>
<font color="#008000">#include <stdio.h><br>
<br>
#include <conio.h><br>
<br>
#include <winsock.h></font><br>
<br>
<br>
<br>
<br>
<br>
<strong>void</strong> main(int argc, char * argv[])<br>
<br>
<strong>{</strong><br>
<br>
<br>
<br>
&nbsp;&nbsp; <strong>if</strong>(argc==2)<br>
<br>
&nbsp;&nbsp;&nbsp; <strong>{</strong><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WSADATA wsaData<strong>;</strong><br>
<br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>if</strong>(WSAStartup(MAKEWORD( 1, 1 ),<br>
&amp;wsaData)==0)<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>{</strong><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#FF0000"><br>
/* Define addr here */</font><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>struct</strong><br>
hostent *hostinfo=gethostbyaddr(addr, 4, AF_INET)<strong>;</strong><br>
<br>
<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>if</strong><br>
(hostinfo!=NULL)<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>{</strong><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
printf("%s", hostinfo->h_name)<strong>;</strong><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>}</strong><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>else</strong><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>{</strong><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>
printf("%s", "error")<strong>;</strong><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>}</strong><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>}</strong><br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getche()<strong>;</strong><br>
<br>
&nbsp;&nbsp;&nbsp; <strong>}</strong><br>
<br>
<strong>}</strong></p>


Edit Report
Re: Winsock: GetHostByAddr (source code) Posted by Phreedom on 7 Aug 1999 at 2:38 PM
Damn HTML parsing!<p>
#include <stdio.h><br>
#include <conio.h><br>
#include <winsock.h><p>
<br>
void main(int argc, char * argv[])<br>
{<br>
if (argc==2)<br>
{<br>
WSADATA wsaData;<br>
if (WSAStartup(MAKEWORD( 1, 1 ), &wsaData)==0)<br>
{<br>
/* Define addr here */<br>
struct hostent *hostinfo=gethostbyaddr(addr, 4, AF_INET);<br>
if (hostinfo!=NULL)<br>
{<br>
printf("%s", hostinfo->h_name);<br>
}<br>
else<br>
{<br>
printf("%s", "error");<br>
}<br>
}<br>
getche();<br>
}<br>
}


Edit Report
Re: Winsock: GetHostByAddr (includes) Posted by Phreedom on 7 Aug 1999 at 2:39 PM
includes: <br>
stdio.h<br>
conio.h<br>
winsock.h


Edit Report
Re: Winsock: GetHostByAddr (includes) Posted by Phreedom on 7 Aug 1999 at 2:39 PM
includes: <br>
stdio.h<br>
conio.h<br>
winsock.h





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.