An example that shows how to receive a file descriptor over librpc connection.
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
int
main(int argc, const char *argv[])
{
int fds[2];
if (argc < 2) {
fprintf(stderr, "Usage: fd-client <server socket URI>\n");
return (1);
}
if (client == NULL) {
fprintf(stderr, "cannot connect: %s\n", strerror(errno));
return (1);
}
if (pipe(fds) != 0) {
fprintf(stderr, "cannot create pipe: %s\n", strerror(errno));
return (1);
}
close(fds[1]);
for (;;) {
char buf[1024];
ssize_t ret;
ret = read(fds[0], buf, sizeof(buf));
if (ret <= 0)
break;
printf("%.*s", (int)ret, buf);
}
return (0);
}