请问如何利用SOCKET解析域名?
比如我的一台服务器是绑定了域名,那些客户端想和服务器进行通信,但只能通过域名来通信。
那该如何利用SOCKET靠域名和服务器通信啊?
- #pragma comment(lib, "ws2_32")
- #include <winsock2.h>
- #include <stdio.h>
- #include <assert.h>
- int main()
- {
- struct hostent *host;
- WSADATA wsaData;
- int ret;
- ret = WSAStartup(0x0202, &wsaData);
- if(ret)
- {
- printf("error in WSAStartup: %d
", WSAGetLastError());
- return 0;
- }
- host = gethostbyname("www.baidu.com");
- if(host == NULL)
- {
- printf("error in gethostbyname: %d
", WSAGetLastError());
- }
- else
- {
- printf("name: %s
addrtype; %d
addrlength: %d
",
- host->h_name, host->h_addrtype, host->h_length);
- printf("ip address: %s
",
- inet_ntoa(*(struct in_addr*)host->h_addr_list[0]));
- }
- WSACleanup();
- return 0;
- }
输出:
name: www.a.shifen.com
addrtype; 2
addrlength: 4
ip address: 202.108.22.5
相关热门文章
给主人留下些什么吧!~~
评论热议