我正在为telnet客户端编写c ++代码。我在从用户输入中获取主机地址时遇到问题。
struct in_addr peers;
cin>>peers;
peerserver = gethostbyaddr((const char*)peers,4,AF_INET);
if (peerserver == NULL)
exit(0);
我是C ++的新手,有人可以建议一种通过用户输入获取主机地址的更好方法。提前致谢。
最佳答案
您要查找的是gethostbyname
,而不是gethostbyaddr
。 gethostbyaddr
假定您已经拥有IP地址。
char peers[256];
cin >> peers;
struct hostent *ent = gethostbyname(peers);
printf("%04x\n", *(int *)(ent->h_addr));
关于c++ - 带有用户输入的C++ gethostbyaddr,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/3878654/