void server(int readfd, int writefd) { FILE *fp; ssize_t n; struct mymesg mesg; /* 4read pathname from IPC channel */ mesg.mesg_type = 1; if ( (n = Mesg_recv(readfd, &mesg)) == 0) err_quit("pathname missing"); mesg.mesg_data[n] = '\0'; /* null terminate pathname */ if ( (fp = fopen(mesg.mesg_data, "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(mesg.mesg_data); 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); }
void server(int readfd, int writefd) { FILE *fp; char *ptr; pid_t pid; ssize_t n; struct mymesg mesg; // iterate server for ( ; ; ) { /* read pathname from IPC channel */ mesg.mesg_type = 1; // need type 1 (client request) 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) { /* error: 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 { /* fopen succeeded: copy file to IPC channel */ while (Fgets(mesg.mesg_data, MAXMESGDATA, fp) != NULL) { // every line is a msg mesg.mesg_len = strlen(mesg.mesg_data); Mesg_send(writefd, &mesg); } Fclose(fp); } /* send a 0-length message to signify the end */ mesg.mesg_len = 0; Mesg_send(writefd, &mesg); } }
void client(int readid, int writeid) { size_t len; ssize_t n; char *ptr; struct mymesg mesg; /* start buffer with msqid and a blank */ snprintf(mesg.mesg_data, MAXMESGDATA, "%d ", readid); len = strlen(mesg.mesg_data); ptr = mesg.mesg_data + len; /* read pathname */ Fgets(ptr, MAXMESGDATA - len, stdin); len = strlen(mesg.mesg_data); if (mesg.mesg_data[len-1] == '\n') len--; /* delete newline from fgets() */ mesg.mesg_len = len; mesg.mesg_type = 1; /* write msqid and pathname to server's well-known queue */ Mesg_send(writeid, &mesg); /* read from our queue, write to standard output */ while ( (n = Mesg_recv(readid, &mesg)) > 0) Write(STDOUT_FILENO, mesg.mesg_data, n); }
void client(int readfd, int writefd) { size_t len; ssize_t n; struct mymesg mesg; /* 4read pathname */ Fgets(mesg.mesg_data, MAXMESGDATA, stdin); len = strlen(mesg.mesg_data); if (mesg.mesg_data[len-1] == '\n') len--; /* delete newline from fgets() */ mesg.mesg_len = len; mesg.mesg_type = 1; /* 4write pathname to IPC channel */ Mesg_send(writefd, &mesg); /* 4read from IPC, write to standard output */ while ( (n = Mesg_recv(readfd, &mesg)) > 0) Write(STDOUT_FILENO, mesg.mesg_data, n); }