Link to home
Start Free TrialLog in
Avatar of outcast
outcast

asked on

About gethostbyname() in winsock.

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 https://www.experts-exchange.com is active).

thanks in advance.
Avatar of ajitm
ajitm

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
By entries I meant was atleast name of computer & its I.P.Address.
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?
   
Avatar of outcast

ASKER

Perhaps my statement is not clear. My OS is Win95.
Anyway, many thanks.
Avatar of outcast

ASKER

I had tested alexo's code. and the result:
pHost is still NULL,
rc = -1,
(name = "https://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.
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;
}

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


Avatar of outcast

ASKER

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

Avatar of outcast

ASKER

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.
Avatar of outcast

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of alexo
alexo
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial