原 socket通訊實現網域名稱解析

2021-09-13 01:39:16 字數 3097 閱讀 1115

//extern     unsigned char gsntpserverip[20];

int getntptime(unsigned char *ntpserverip,unsigned int ntpport,unsigned int *data)

sockfd = socket(af_inet, sock_dgram, 0);

if(sockfd<0)

#endif

#if 1//getaddrinfo可以解析網域名稱的方式

int rc;

addrinfo hints, *res = null,*cur=null;

memset(&hints, 0, sizeof(hints));

hints.ai_family = af_unspec;

hints.ai_socktype = sock_dgram;

hints.ai_protocol = ipproto_udp;

char port[10];

memset(port,0,sizeof(port));

sprintf(port,"%d",ntpport);

rc = getaddrinfo((const char*)ntpservername, port, &hints, &res);

if (rc != 0)

struct sockaddr_in *addr;

char m_ipaddr[16];

memset(m_ipaddr,0,sizeof(m_ipaddr));

for (cur = res; cur != null; cur = cur->ai_next)

sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

if (sockfd < 0)

#endif

//if(sendto(sockfd, (void*)&h_ntp, sizeof(stnp_header), 0, (struct sockaddr*)&server, sizeof(server))<0)

if(sendto(sockfd, (void*)&h_ntp, sizeof(stnp_header), 0, res->ai_addr, res->ai_addrlen)<0)

fd_set r;

fd_zero(&r);

fd_set(sockfd, &r);

struct timeval timeout;

timeout.tv_sec = 10;

timeout.tv_usec = 0;

if(1 != select(sockfd+1, &r, null, null, &timeout))

if(recv(sockfd, (void*)data, 48, 0)<0) //48 後續要定義成巨集

close(sockfd);

return 0;

}

getaddrinfo函式

使用到的結構體理解

addrinfo

sockaddr_in

/* internet address.  */

typedef __u16 in_port_t;

typedef __u32 in_addr_t;

struct in_addr

;/* structure describing an internet socket address.  */

struct sockaddr_in

;

struct sockaddr_in *addr;

char m_ipaddr[16];

memset(m_ipaddr,0,sizeof(m_ipaddr));

for (cur = res; cur != null; cur = cur->ai_next)

需要區分下這些結構體的差異

sockaddr_in 用於ipv4的網路internet ip/port,所以其結構成員sin_family必須填入af_inet??

struct sockaddr_in server;

server.sin_family = af_inet;

server.sin_addr.s_addr = gethostipbyname((const char*)ntpservername);

server.sin_port = htons(ntpport);

if(-1 == (int)server.sin_addr.s_addr)

addrinfo 

addrinfo hints, *res = null,*cur=null;

memset(&hints, 0, sizeof(hints));

hints.ai_family = af_unspec;

hints.ai_socktype = sock_dgram;

hints.ai_protocol = ipproto_udp;

char port[10];

memset(port,0,sizeof(port));

sprintf(port,"%d",ntpport);

rc = getaddrinfo((const char*)ntpservername, port, &hints, &res);

根據位址域獲取到的資訊存放在res

sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);

if (sockfd < 0)

if(sendto(sockfd, (void*)&h_ntp, sizeof(ntp_header), 0, res->ai_addr, res->ai_addrlen)<0)

待確認:

gethostipbyname 不能解析位址域嗎???當時之所以不能解析的原因是由於沒有新增dns伺服器在resolv.conf

-----------由搬遷過來。by 琳麻雀

socket通訊實現

為了實現socket的通訊驗證,寫了乙個簡單的客服端向伺服器端通訊的 伺服器端 1 版本驗證 word wversionrequested makeword 2 2 wsadata result int err wsastartup wversionrequested,result if err 0...

實現網域名稱解析

編寫程式,使用gethostbyname,getaddrinfo。不過你必須保證自己裝置板子的dns配置部分是正常的。不一定完全正確,我簡單測試了一下,可以用 主要是 bool te getrealip char hostname,char strrealip 輸入的hostname可以是ip,也可...

Linux下socket網域名稱解析的實現

目錄 網域名稱解析 linux下socket網域名稱解析 網域名稱解析的具體函式 gethostbyname函式 getaddrinfo函式 getnameinfo函式 程式設計例項 執行結果 ping baidu.comlinux作業系統 windows作業系統 在我們寫網路socket的客戶端的...