在linux中创建名称管道并在c-app和dlang之间交换struct数据

时间:2016-12-05 10:26:01

标签: c d

我正在尝试在现有项目中使用dlang用Web GUI替换当前的c编码应用程序GUI。该应用程序仍然存在于OpenSuse 12.3 x32平台上,并通过FIFO管道与GUI通信。

消息是在C中创建的结构,在一个.h文件中定义,可以为项目中的所有各方全局访问。

现在我的问题,我已经设法创建一个管道并写入它并从一个带有“cat”的终端读取管道的内容,但是当我尝试在dlang中使用管道句柄读取时,我得到了一个很多?-chars和返回信息-1从管道读取。任何想法为什么“?????”是我收到的 下面的代码首先是服务器

import std.range, std.stdio, std.conv;
import core.stdc.string;
import core.stdc.stdio;
import core.sys.posix.sys.stat;
import core.sys.posix.unistd;
import core.sys.posix.fcntl;
import core.sys.posix.sys.stat;

//extern (C) uint read(int, void *, uint);
extern (C) uint write(int, void *, ulong);

struct IOREQ {
    short   fc;         /* function code */
    short   rs;         /* return code */
    short   src;        /* return in fifo-pipe */
 };

int fd = -1;
int freader = -1;
char [1024] rbuf = "";



void main(string[] args) {
int st = mknod("/Users/anders/pipes/4321", S_IFIFO|octal!666,0);
close(st);
fd = open("/Users/anders/pipes/4321", O_WRONLY);
//freader = open("/Users/anders/pipes/4321", O_RDONLY);

char [1024]sbuf;
char [1024]rbuf;        
int res = -1;
sbuf =  "Testing a message in pure text to send to client using a FIFO-pipe\n";
while(1){

    res = write(fd, cast(void *)sbuf, sbuf.sizeof);
    writeln("Wrote data in the pipe, :", res);
    sleep(2);
    sbuf =  "Testing another message in pure text to send to client\n";
}

close(fd);

}

现在客户端

import std.range, std.stdio, std.conv;
import core.stdc.string;
import core.stdc.stdio;
import core.sys.posix.sys.stat;
import core.sys.posix.unistd;
import core.sys.posix.fcntl;
import core.sys.posix.sys.stat;
import core.thread;

extern (C) uint read(int, void *, uint);
//extern (C) uint write(int, void *, ulong);

struct IOREQ {
...
 };

int fd = -1;
int freader = -1;
char [1024] rbuf = "";



void main(string[] args) {
//int st = mknode("/Users/anders/pipes/4321", S_FIFO|0666,0);

freader = open("/Users/anders/pipes/4321", O_RDONLY);
//freader = open("/Users/anders/pipes/4321", O_RDONLY);
if(freader < 0)
{
    writeln("Couldn't open pipe ", freader ); 
}
else
{
    char [1024]sbuf;
    char [1024]rbuf;        

    writeln("Reading data from pipeserver\n");
    int st = read(fd, cast(void *)sbuf, sbuf.sizeof);
    writeln("Done reading, read :", st);
    writeln("Data :\n", sbuf);


        close(freader);
    }
}

1 个答案:

答案 0 :(得分:0)

我们在瑞典发现了SBS的问题。我正在从错误的文件/管道读取纠正文件处理程序读取到freader而不是fd。还要添加import core.stdc.errno; writeln(errno)给了我errorno的输出。 http://virtsync.com/c-error-codes-include-errno显示内容