Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

About gethostbyname() in winsock.

Posted on 1999-08-02
11
Medium Priority
?
347 Views
Last Modified: 2008-03-06
I could never use this function successfully either in my
own program or others' example, and the WSAGetLastError()
returns 11001(Host not found). I don't know what's wrong.
Is it due to my computer or net? but the following should be
correct.
1. my network configure(such as DNS, IP address, Gateway);
2. TCP/IP protocol has been installed ;
3. I can reach the appointed host(ping www.experts-exchange.com is active).

thanks in advance.
0
Comment
Question by:outcast
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 1

Expert Comment

by:ajitm
ID: 1201583
I think there should have entries of atleast local m/cs on your LAN in your etc/hosts file. If your OS is NT then you can find this folder in winnt\system32\drivers\etc\hosts. Or else find for etc. Here you should have entry for your default gateway.

Ajit
0
 
LVL 1

Expert Comment

by:ajitm
ID: 1201584
By entries I meant was atleast name of computer & its I.P.Address.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1201585
First try this piece of code:

    WSADATA data;
    char name[256];
    struct hostent *pHost;
    int rc;

    rc = WSAStartup(MAKEWORD(1, 1), &data);
    rc = gethostname(name, sizeof name);
    pHost = gethostbyname(name);

(run single-step and check result codes).

Does it work?

Do you have a valid DNS configured?
   
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:outcast
ID: 1201586
Perhaps my statement is not clear. My OS is Win95.
Anyway, many thanks.
0
 

Author Comment

by:outcast
ID: 1201587
I had tested alexo's code. and the result:
pHost is still NULL,
rc = -1,
(name = "www.experts-exchange.com" , "www.aol.com" or any
other host which I could reach)
my DNS is doubtless valid. otherwise I can hardly logon
this site.

thanks again.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1201588
Whoa, my code was supposed to check if the name/address of the *local* machine is resolved correctly.

Two versions:

////////////////////////////////////////////////////////////////////////////////

#include <winsock.h>
#pragma comment(lib, "wsock32.lib")

int main()
{
    WSADATA data;
    char name[256];
    struct hostent *pHost;
    int rc;

    rc = WSAStartup(MAKEWORD(1, 1), &data);
    rc = gethostname(name, sizeof name);
    pHost = gethostbyname(name);

    return 0;
}

////////////////////////////////////////////////////////////////////////////////

#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")

int main()
{
    WSADATA data;
    char name[256];
    struct hostent *pHost;
    int rc;

    rc = WSAStartup(MAKEWORD(2, 2), &data);
    rc = gethostname(name, sizeof name);
    pHost = gethostbyname(name);

    return 0;
}

////////////////////////////////////////////////////////////////////////////////


0
 

Author Comment

by:outcast
ID: 1201589
Hi,alexo:
sorry for my misunderstanding. Test your code again and it
works well except for that h_aliases is NULL, but I think
it make no difference. And If I change "name" to a remote
host it still return err. what's the problem? Can your help
me?
many thanks
0
 
LVL 11

Expert Comment

by:alexo
ID: 1201590
>> Can your help me?
I can try...

Are you on a LAN?  Do you dial up?  Can you connect to other sites with a web browser or another client?

0
 

Author Comment

by:outcast
ID: 1201591
To alexo:

>>Are you on a LAN?
Yes.
>>Do you dial up?
No, but DDN E1 line.
>>Can you connect to other sites  with a web browser or another client?
Yes, I can. with either browser or any other client.
0
 

Author Comment

by:outcast
ID: 1201592
hi, alexo:

all right. it works. Thank you very much.
I find I have made a mistake compared with your code which
I ignored yesterday. your codes like this,

#include <winsock.h>
#pragma comment(lib, "wsock32.lib") "

and mine are:

"#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")

That's the catch, but what's the difference?

BTW: How can I submit a FORM to accept your suggestion ?
now I can only add a comment here.
0
 
LVL 11

Accepted Solution

by:
alexo earned 300 total points
ID: 1201593
There are two differents versions of winsock, each one has its library and its include file (that's why I gave you two versions of the program, above).  You cannot mix the include file of winsock 1.x with the library of winsock 2.x and vice versa.  You should also make sure you request the right version in the call to WSAStartup().

You can accept an "answer".  Up until now I only submitted "comments" because I wasn't sure how to taclke your problem.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question