Created
March 1, 2018 00:03
-
-
Save tehp/f7fa378106e555edc009e877ff5c221e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mesg.h" | |
void client(int readfd, int writefd) { | |
size_t len; | |
ssize_t n; | |
struct mymesg mesg; | |
Fgets(mesg.mesg_data, MAXMESGDATA, stdin); | |
len = strlen(mesg.mesg_data); | |
if (mesg.mesg_data[len - 1] == '\n') { | |
len--; | |
} | |
mesg.mesg_len = len; | |
mesg.mesg_type = 1; | |
Mesg_send(writefd, &mesg); | |
while ((n = Mesg_recv(readfd, &mesg)) > 0) { | |
Write(STDOUT_FILENO, mesg.mesg_data, n); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "unpipc.h" | |
#define MAXMESGDATA (PIPE_BUF - 2*sizeof(long)) | |
#define MESGHDRSIZE (sizeof(struct mymesg) - MAXMESGDATA) | |
struct mymesg { | |
long mesg_len; | |
long mesg_type; | |
char mesg_data[MAXMESGDATA]; | |
}; | |
ssize_t mesg_send(int, struct mymesg *); | |
void Mesg_send(int, struct mymesg *); | |
ssize_t mesg_recv(int, struct mymesg *); | |
ssize_t Mesg_recv(int, stuct mymesg *); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mesg.h" | |
ssize_t mesg_recv (int fd, struct mymesg *mptr) { | |
size_t len; | |
ssize_t n; | |
if ((n = Read(fd, mptr, MESGHDRSIZE)) == 0) { | |
return (0); | |
} | |
else if (n != MESGHDRSIZE) { | |
err_quit("message header: expected %d, got %d", MESGHDRSIZE, n); | |
} | |
if ((len = mptr->mesg_len) > 0) { | |
if ((n = Read(fd, mptr->mesg_data, len)) != len) { | |
err_quit("message data: expected %d, got %d",len, n); | |
} | |
} | |
return len; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mesg.h" | |
ssize_t mesg_send (int fd, struct mymesg *mptr) { | |
return (write(fd, mptr, MESGHDRSIZE + mptr->mesg_len)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void server(int readfd, int writefd) { | |
FILE *fp; | |
ssize_t n; | |
struct mymesg mesg; | |
mesg.mesg_type = 1; | |
if ((n = Mesg_recv(readfd, &mesg)) == 0) { | |
err_quit("pathname missing"); | |
} | |
mesg.mesg_data[n] = '\0'; | |
if ((fp = fopen(mesg.mesg_data, "r")) == NULL) { | |
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 { | |
while (Fgets(mesg.mesg_data, MAXMESGDATA, fp) != NULL) { | |
mesg.mesg_len = strlen(mesg.mesg_data); | |
Mesg_send(writefd, &mesg); | |
} | |
Fclose(fp); | |
} | |
mesg.mesg_len = 0; | |
Mesg_send(writefd, &mesg); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment