I am writing c++ code for a telnet client. I am having problems getting the host address from the user input.
struct in_add开发者_运维问答r 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.
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));
精彩评论