我有以下代码可以正常工作。
FILE *pipe_fp;
if((pipe_fp = popen("php", "w")) == NULL) {
perror("popen(PHP)");
exit(1);
}
fputs("<?php echo process('xx'); ?>", pipe_fp);
int ret = pclose(pipe_fp);
if(WIFEXITED(ret))
printf("%d\n", WEXITSTATUS(ret));
问题是当我尝试这样的事情时:
// up to this point i am starting a socket servers and wait for clients
int bSize;
int nSize;
char buffer[MAXBUF+1];
char receive[MAXBUF+1];
while(1) {
bSize = recv(new_fd, buffer, MAXBUF, 0);
if(bSize > 0) {
buffer[bSize] = '\0';
strcat(receive, buffer);
}
}
// I rote this part based on this post: http://stackoverflow.com/questions/1383649/concatenating-strings-in-c-which-method-is-more-efficient
char * first= "<?php echo process('";
char * second = "'); ?>";
char * code = NULL;
asprintf(&code, "%s%s%s", first, receive, second);
// the above code somes here, i just copied the part that has changed
fputs(code, pipe_fp);
我尝试了许多其他示例,都导致失败。我在 C 3 天大。