ga_serv.c

来自「Unix网络编程 基于Socket的网络编程」· C语言 代码 · 共 59 行

C
59
字号
#include	"gai_hdr.h"/* * This function handles the service string. *//* include ga_serv */intga_serv(struct addrinfo *aihead, const struct addrinfo *hintsp,		const char *serv){	int				port, rc, nfound;	struct servent	*sptr;	nfound = 0;	if (isdigit(serv[0])) {		/* check for port number string first */		port = htons(atoi(serv));		if (hintsp->ai_socktype) {				/* 4caller specifies socket type */			if ( (rc = ga_port(aihead, port, hintsp->ai_socktype)) < 0)				return(EAI_MEMORY);			nfound += rc;		} else {				/* 4caller does not specify socket type */			if ( (rc = ga_port(aihead, port, SOCK_STREAM)) < 0)				return(EAI_MEMORY);			nfound += rc;			if ( (rc = ga_port(aihead, port, SOCK_DGRAM)) < 0)				return(EAI_MEMORY);			nfound += rc;		}	} else {			/* 4try service name, TCP then UDP */		if (hintsp->ai_socktype == 0 || hintsp->ai_socktype == SOCK_STREAM) {			if ( (sptr = getservbyname(serv, "tcp")) != NULL) {				if ( (rc = ga_port(aihead, sptr->s_port, SOCK_STREAM)) < 0)					return(EAI_MEMORY);				nfound += rc;			}		}		if (hintsp->ai_socktype == 0 || hintsp->ai_socktype == SOCK_DGRAM) {			if ( (sptr = getservbyname(serv, "udp")) != NULL) {				if ( (rc = ga_port(aihead, sptr->s_port, SOCK_DGRAM)) < 0)					return(EAI_MEMORY);				nfound += rc;			}		}	}	if (nfound == 0) {		if (hintsp->ai_socktype == 0)			return(EAI_NONAME);	/* all calls to getservbyname() failed */		else			return(EAI_SERVICE);/* service not supported for socket type */	}	return(0);}/* end ga_serv */

⌨️ 快捷键说明

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