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

📄 sockaddr.c

📁 Path MPICH-V for MPICH the MPI Implementation
💻 C
字号:
/* sockaddr.c   Copyright (c) 2002 Victor C. Zandy <zandy@cs.wisc.edu>   This program is free software; you can redistribute it and/or   modify it under the terms of the GNU General Public License as   published by the Free Software Foundation; either version 2, or (at   your option) any later version.  See the file COPYING for details. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <assert.h>#include <sys/socket.h>#include <netinet/ip.h>#include <arpa/inet.h>    /* inet_* */#include <netdb.h>        /* host,service names */char *ipstr(struct sockaddr_in *sa){	static char buf[128];	struct servent *servent;	static char addr[32];	static char serv[32];	strcpy(addr, inet_ntoa(sa->sin_addr));	servent = getservbyport(sa->sin_port, "tcp");	if (servent) {		strcpy(serv, servent->s_name);		sprintf(buf, "%s:%s", addr, serv);	}	else		sprintf(buf, "%s:%d", addr, ntohs(sa->sin_port));	return buf;}static voidnomem(){	fprintf(stderr, "Out of memory\n");	exit(1);}static intparse_addr(const char *s, struct in_addr *addr){	struct hostent* h;	h = gethostbyname(s);	if (!h)		return -1;	*addr = *((struct in_addr *) h->h_addr); /* network order */	return 0;}static intparse_port(const char *s, short *port){	char *p;	struct servent *se;	unsigned long l;	se = getservbyname(s, "tcp");	if (se) {		*port = se->s_port;		return 0;	}	l = strtoul(s, &p, 10);	if (*p != '\0')		return -1;	*port = (short) htons(l);	return 0;}intparse_ip(const char *s, struct sockaddr_in *addr){	char *buf = NULL;	char *p;	int ret = -1;	buf = strdup(s);	if (!buf)		nomem();	addr->sin_family = AF_INET;	addr->sin_addr.s_addr = INADDR_ANY;	addr->sin_port = htons(0);	if ((p = strchr(buf, ':'))) {		/* HOST:PORT */		*p++ = '\0';		if (0 > parse_addr(buf, &addr->sin_addr))			goto out;		if (0 > parse_port(p, &addr->sin_port))			goto out;	} else if ((p = strchr(buf, '.'))) {		/* HOST */		if (0 > parse_addr(buf, &addr->sin_addr))			goto out;	} else {		/* PORT or HOST? */		if (0 > parse_port(buf, &addr->sin_port)		    && 0 > parse_addr(buf, &addr->sin_addr))			goto out;	}	ret = 0;out:	free(buf);	return ret;}

⌨️ 快捷键说明

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