blob: c53df291ed5b740d73dfb3b8fb810f86cc2e8563 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
int main() {
FILE *log = fopen("log.txt", "w");
fprintf(log, "hello\n"); fflush(log);
for (;;) {
char *outMsg = "{\"text\":\"This is a response message\"}";
unsigned int outLen = strlen(outMsg);
char *bOutLen = (char *)&outLen;
write(1, bOutLen, 4); // 1 is stdout
write(1, outMsg, outLen);
fflush(stdout);
fprintf(log, "wrote msg\n"); fflush(log);
char bInLen[4];
read(0, bInLen, 4); // 0 is stdin
unsigned int inLen = *(unsigned int *)bInLen;
char *inMsg = (char *)malloc(inLen);
read(0, inMsg, inLen);
inMsg[inLen] = '\0';
fprintf(log, "msg: [%s]\n", inMsg); fflush(log);
free(inMsg);
}
return 0;
}
|