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

📄 tcp_listen.c

📁 linux下的tcp通信多线程示例,通过创建多线程来建立TCP联接.在嵌入式系统下运行
💻 C
字号:
/* include tcp_listen *///#include	"unp.h"#include	"tcp_listen.h"inttcp_listen(const char *host, const char *serv, socklen_t *addrlenp){	int				listenfd, n;	const int		on = 1;	struct addrinfo	hints, *res, *ressave;	bzero(&hints, sizeof(struct addrinfo));//	hints.ai_flags = AI_PASSIVE;//	hints.ai_family = AF_UNSPEC;	hints.ai_socktype = SOCK_STREAM;	if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0){		printf("tcp_listen error for %s, %s: %s",				 host, serv, gai_strerror(n));	//	perror("tcp_listen error");		exit(0);	}#if 0	err_quit("tcp_listen error for %s, %s: %s",				 host, serv, gai_strerror(n));#endif	ressave = res;	do {		listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);		if (listenfd < 0)			continue;		/* error, try next one */		setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));		if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)			break;			/* success */		close(listenfd);	/* bind error, close and try next one */	} while ( (res = res->ai_next) != NULL);	if (res == NULL)	/* errno from final socket() or bind() */		perror("tcp_listen error");		//err_sys("tcp_listen error for %s, %s", host, serv);	//listen(listenfd, LISTENQ);	listen(listenfd, 16);	if (addrlenp)		*addrlenp = res->ai_addrlen;	/* return size of protocol address */	freeaddrinfo(ressave);	return(listenfd);}/* end tcp_listen *//* * We place the wrapper function here, not in wraplib.c, because some * XTI programs need to include wraplib.c, and it also defines * a Tcp_listen() function. */intTcp_listen(const char *host, const char *serv, socklen_t *addrlenp){	return(tcp_listen(host, serv, addrlenp));}

⌨️ 快捷键说明

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