⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getaddrinfo.c

📁 linux下的socket通信,里面有几个例子
💻 C
字号:

/* 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -