开发者

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_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));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜