thegreattaurus
thegreattaurus

Reputation: 39

c++ gethostbyaddr with user input

I am writing c++ code for a telnet client. I am having problems getting the host address from the user input.

struct in_addr peers;

cin>>peers;

peerserver = gethostbyaddr((const char*)peers,4,AF_INET);

if (peerserver == NULL)
    exit(0);

I am new to c++, can anyone suggest a better way of getting the host addr with user input. Thanks in advance.

Upvotes: 0

Views: 894

Answers (1)

Eric Warmenhoven
Eric Warmenhoven

Reputation: 2962

What you're looking for is gethostbyname, not gethostbyaddr. gethostbyaddr assumes that you've already got the IP address.

char peers[256];
cin >> peers;
struct hostent *ent = gethostbyname(peers);
printf("%04x\n", *(int *)(ent->h_addr));

Upvotes: 1

Related Questions