通过命名管道发送结构化数据 (Linux)

发布于 2024-09-14 05:10:46 字数 682 浏览 5 评论 0原文

我在 Debian 系统上使用命名管道进行 IPC。我将把一些数据作为一组字符串从 bash 脚本发送到用 C 代码编写的后台运行进程。

我要发送的数据是四个字符串,例如 accountid、名字、姓氏、描述。目前,我将数据作为 bash 脚本中用空格分隔的字符数组发送。

echo "accountid firstname surname description" >$pipe

在后台进程中,我将这样的管道数据读入字符数组“datain”中

res = read(pipe_fd, datain, BUFFER_SIZE);

,然后我只是在数组上迭代,寻找空格

char* p = datain;

char accountid[80];
char firstname[80];

// extract the accountid
while(p!='')
{
    accountid = p;
    ++p;
}

++p;

while(p!='')
{
    firstname = p;
    ++p;
}

......

这种方法似乎有点粗糙,但是我的编程技能不是那么好,所以我想知道是否有更好的策略通过 Linux 中的命名管道传输这组数据。

谢谢

i am using a named pipe for IPC on a Debian system. I will be sending some data as a set of strings from a bash script to a background running process written in C code.

The data i want to send is four strings eg accountid, firstname,surname, description. Currently i am sending the data as a char array separated by spaces from my bash script.

echo "accountid firstname surname description" >$pipe

In the background process i read the pipe data like this into char array 'datain'

res = read(pipe_fd, datain, BUFFER_SIZE);

then i am just iterating over the array looking for spaces

eg

char* p = datain;

char accountid[80];
char firstname[80];

// extract the accountid
while(p!='')
{
    accountid = p;
    ++p;
}

++p;

while(p!='')
{
    firstname = p;
    ++p;
}

etc....

This method seems a bit crude however my programming skills are not that good so i was wondering if there was a better strategy for transferring this set of data over a named pipe in Linux.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩人痒 2024-09-21 05:10:46
  • 管道(命名或未命名)是字节流。如果双方使用相同的语言,则可能有更好的方法来发送结构化数据。在您的情况下,像您所做的那样,手动编码和解码是迄今为止最简单的解决方案。

  • 不要使用空格来分隔可能包含空格的字段,例如人名。使用 :,例如 /etc/passwd

  • 在 C 中,read 很难使用,因为您必须提前决定缓冲区大小并且您必须在循环中调用它,因为它可能会返回突发奇想小于缓冲区大小。 stdio.h 中的函数(作用于 FILE* 而不是文件描述符)更易于使用,但仍需要处理长行。如果您不关心 Linux 之外的可移植性,请使用 getline

    FILE *pipe = fdopen(fd, "r");
    字符*行= NULL;
    size_t 线长度;
    getline(&line, &line_length, 管道);
    

然后使用 strchr 定位行中的 :。 (不要尝试使用 strtok,它仅适用于不能为空的空格分隔字段。)

  • A pipe (named or not) is a stream of bytes. If you were using the same language on both sides, there might be a better way of sending structured data. In your situation, a manual encoding and decoding, like you're doing, is by far the easiest solution.

  • Don't use spaces to separate fields that may contain spaces, such as people's names. Use :, like /etc/passwd.

  • In C, read is hard to use, because you have to decide on a buffer size in advance and you have to call it in a loop because it may return less than the buffer size on a whim. The functions from stdio.h (that operate on a FILE* rather than a file descriptor) are easier to use but still require work to handle long lines. If you don't care about portability outside Linux, use getline:

    FILE *pipe = fdopen(fd, "r");
    char *line = NULL;
    size_t line_length;
    getline(&line, &line_length, pipe);
    

Then use strchr to locate the :s in the line. (Don't be tempted to use strtok, it's only suitable for whitespace-separated fields that can't be empty.)

你与清晨阳光 2024-09-21 05:10:46

自 2010 年起,您可能希望以 JSON 或 XML 形式对数据进行编码,这两种语言都可以作为 C 语言和几乎任何其他语言的库轻松使用。

Since it's 2010, you might want to encode your data in JSON or XML, both of which are readily available as libraries for C and almost any other language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文