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

📄 server.c

📁 unix网络编程的源码
💻 C
字号:
#include	"mesg.h"

void
server(int readfd, int writefd)
{
	FILE	*fp;
	char	*ptr;
	pid_t	pid;
	ssize_t	n;
	struct mymesg	mesg;

	for ( ; ; ) {
			/* 4read pathname from IPC channel */
		mesg.mesg_type = 1;
		if ( (n = Mesg_recv(readfd, &mesg)) == 0) {
			err_msg("pathname missing");
			continue;
		}
		mesg.mesg_data[n] = '\0';	/* null terminate pathname */

		if ( (ptr = strchr(mesg.mesg_data, ' ')) == NULL) {
			err_msg("bogus request: %s", mesg.mesg_data);
			continue;
		}

		*ptr++ = 0;			/* null terminate PID, ptr = pathname */
		pid = atol(mesg.mesg_data);
		mesg.mesg_type = pid;	/* for messages back to client */

		if ( (fp = fopen(ptr, "r")) == NULL) {
				/* 4error: must tell client */
			snprintf(mesg.mesg_data + n, sizeof(mesg.mesg_data) - n,
					 ": can't open, %s\n", strerror(errno));
			mesg.mesg_len = strlen(ptr);
			memmove(mesg.mesg_data, ptr, mesg.mesg_len);
			Mesg_send(writefd, &mesg);
	
		} else {
				/* 4fopen succeeded: copy file to IPC channel */
			while (Fgets(mesg.mesg_data, MAXMESGDATA, fp) != NULL) {
				mesg.mesg_len = strlen(mesg.mesg_data);
				Mesg_send(writefd, &mesg);
			}
			Fclose(fp);
		}
	
			/* 4send a 0-length message to signify the end */
		mesg.mesg_len = 0;
		Mesg_send(writefd, &mesg);
	}
}

⌨️ 快捷键说明

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