我怎样才能将输出作为另一个c文件的输出?

时间:2021-08-08 06:00:58

So for example, let say I have a file server.c which is not printing anything but has a string e.g: "Fish is swimming in the air". What I want to do is to let child.c to print the string of server.c Is that even possible? I was told that using pipelines (such popen()) would help. But I can't find what I want out there.

例如,假设我有一个文件server.c,它不打印任何东西,但有一个字符串,例如:“鱼在空中游泳”。我想要做的是让child.c打印server.c的字符串甚至可能吗?我被告知使用管道(如popen())会有所帮助。但我找不到我想要的东西。

1 个解决方案

#1


2  

I'm sure there is possibly a way to do that using pipe functions (check something like this site unixwiz.net/techtips/remap-pip-fds.html) but what you are describing sounds like another client connecting to the server and having the strings sent to it across a socket. Using a socket would also open up the ability to check the server string over a network. Typically with error/extra log checking with a server it is either handled by the server opening a log file or by sending it across a socket. You could determine to send it to a specific client that uses PSK over a TLS connection if security is an issue.

我确信有可能使用管道功能(检查类似于这个网站unixwiz.net/techtips/remap-pip-fds.html)的方法,但你所描述的听起来像另一个客户端连接到服务器和拥有通过套接字发送给它的字符串。使用套接字还可以打开通过网络检查服务器字符串的功能。通常使用服务器进行错误/额外日志检查,它由服务器打开日志文件或通过套接字发送来处理。如果安全性存在问题,您可以确定将其发送到通过TLS连接使用PSK的特定客户端。

For TLS examples check out https://github.com/wolfSSL/wolfssl-examples

有关TLS示例,请查看https://github.com/wolfSSL/wolfssl-examples

Added in code for piping

在管道代码中添加

receiver.c

receiver.c

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 
  5 int main() {
  6 
  7     char buffer[1024];
  8 
  9     fscanf(stdin, "%s", buffer);
 10 
 11     printf("receiver got data and is printing it\n");
 12     printf("%s\n", buffer);
 13 
 14     return 0;
 15 }

sender.c

sender.c

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 
  5 
  6 int main()
  7 {
  8     FILE *output;
  9 
 10     output = popen ("./receiver","w");
 11     if (!output) {
 12         /* error checking opening pipe */
 13         fprintf(stderr, "could not open pipe\n");
 14         return 1;
 15     }
 16 
 17     fprintf(output, "%s", "hello_world\n");
 18 
 19     if (pclose (output) != 0) {
 20         /* error checking on closing pipe */
 21         fprintf(stderr, " could not run receiver\n");
 22         return 1;
 23     }
 24 
 25     return 0;
 26 }

compile and run in the same directory using

使用编译并在同一目录中运行

gcc sender.c -o sender
gcc receiver.c -o receiver
./sender

#1


2  

I'm sure there is possibly a way to do that using pipe functions (check something like this site unixwiz.net/techtips/remap-pip-fds.html) but what you are describing sounds like another client connecting to the server and having the strings sent to it across a socket. Using a socket would also open up the ability to check the server string over a network. Typically with error/extra log checking with a server it is either handled by the server opening a log file or by sending it across a socket. You could determine to send it to a specific client that uses PSK over a TLS connection if security is an issue.

我确信有可能使用管道功能(检查类似于这个网站unixwiz.net/techtips/remap-pip-fds.html)的方法,但你所描述的听起来像另一个客户端连接到服务器和拥有通过套接字发送给它的字符串。使用套接字还可以打开通过网络检查服务器字符串的功能。通常使用服务器进行错误/额外日志检查,它由服务器打开日志文件或通过套接字发送来处理。如果安全性存在问题,您可以确定将其发送到通过TLS连接使用PSK的特定客户端。

For TLS examples check out https://github.com/wolfSSL/wolfssl-examples

有关TLS示例,请查看https://github.com/wolfSSL/wolfssl-examples

Added in code for piping

在管道代码中添加

receiver.c

receiver.c

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 
  5 int main() {
  6 
  7     char buffer[1024];
  8 
  9     fscanf(stdin, "%s", buffer);
 10 
 11     printf("receiver got data and is printing it\n");
 12     printf("%s\n", buffer);
 13 
 14     return 0;
 15 }

sender.c

sender.c

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <unistd.h>
  4 
  5 
  6 int main()
  7 {
  8     FILE *output;
  9 
 10     output = popen ("./receiver","w");
 11     if (!output) {
 12         /* error checking opening pipe */
 13         fprintf(stderr, "could not open pipe\n");
 14         return 1;
 15     }
 16 
 17     fprintf(output, "%s", "hello_world\n");
 18 
 19     if (pclose (output) != 0) {
 20         /* error checking on closing pipe */
 21         fprintf(stderr, " could not run receiver\n");
 22         return 1;
 23     }
 24 
 25     return 0;
 26 }

compile and run in the same directory using

使用编译并在同一目录中运行

gcc sender.c -o sender
gcc receiver.c -o receiver
./sender