getaddrinfo.c

来自「linux下的socket通信,里面有几个例子」· C语言 代码 · 共 37 行

C
37
字号

/* getaddrinfo.c */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>

int main()
{
	struct addrinfo hints, *res = NULL;
	int rc;
	
	memset(&hints, 0, sizeof(hints));
	/*设置addrinfo结构体中各参数 */
	hints.ai_flags = AI_CANONNAME;
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	hints.ai_protocol = IPPROTO_UDP;
	
	/*调用getaddinfo函数*/
	rc = getaddrinfo("localhost", NULL, &hints, &res);
	if (rc != 0) 
	{
		perror("getaddrinfo");
		exit(1);
	}
	else
	{
		printf("Host name is %s\n", res->ai_canonname);
	}
	exit(0);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?