请问如何利用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\n", WSAGetLastError());
- return 0;
- }
- host = gethostbyname("");
- if(host == NULL)
- {
- printf("error in gethostbyname: %d\n", WSAGetLastError());
- }
- else
- {
- printf("name: %s\naddrtype; %d\naddrlength: %d\n",
- host->h_name, host->h_addrtype, host->h_length);
- printf("ip address: %s\n",
- inet_ntoa(*(struct in_addr*)host->h_addr_list[0]));
- }
- WSACleanup();
- return 0;
- }
输出:
name:
addrtype; 2
addrlength: 4
ip address: 202.108.22.5
阅读(1553) | 评论(0) | 转发(0) |