fig17.15

来自「unix环境编程」· 15 代码 · 共 48 行

15
48
字号
#include "apue.h"#include <sys/socket.h>#include <sys/un.h>#include <errno.h>#define QLEN	10/* * Create a server endpoint of a connection. * Returns fd if all OK, <0 on error. */intserv_listen(const char *name){	int					fd, len, err, rval;	struct sockaddr_un	un;	/* create a UNIX domain stream socket */	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)		return(-1);	unlink(name);	/* in case it already exists */	/* fill in socket address structure */	memset(&un, 0, sizeof(un));	un.sun_family = AF_UNIX;	strcpy(un.sun_path, name);	len = offsetof(struct sockaddr_un, sun_path) + strlen(name);	/* bind the name to the descriptor */	if (bind(fd, (struct sockaddr *)&un, len) < 0) {		rval = -2;		goto errout;	}	if (listen(fd, QLEN) < 0) {	/* tell kernel we're a server */		rval = -3;		goto errout;	}	return(fd);errout:	err = errno;	close(fd);	errno = err;	return(rval);}

⌨️ 快捷键说明

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