在 linux 中创建名称管道并在 c-app 和 dlang 之间交换结构数据
Creating name pipes and exchanging struct data between c-app and dlang in linux
我正在尝试在现有项目中使用 dlang 将当前的 c 编码应用程序 GUI 替换为 Web GUI。该应用程序仍然存在,在 OpenSuse 12.3 x32 平台上并通过 FIFO 管道与 GUI 通信。
该消息是一种用 C 语言创建的结构,并在一个单一的 .h 文件中定义,项目中的所有各方都可以全局访问。
现在我的问题是,我设法创建了一个管道并向其写入数据,然后使用 "cat " 从终端读取管道的内容,但是当我尝试使用 pipe-handle 进行读取时dlang 我得到了很多 ?-chars 和 return 信息,这些信息表明 -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);
}
}
找到问题了,瑞典的SBS。我正在从错误的地方读取 file/pipe 将读取中的文件处理程序更正为 freader 而不是 fd。还要添加 import core.stdc.errno; writeln(errno) 给了我 errorno 的输出。
http://virtsync.com/c-error-codes-include-errno 显示内容
我正在尝试在现有项目中使用 dlang 将当前的 c 编码应用程序 GUI 替换为 Web GUI。该应用程序仍然存在,在 OpenSuse 12.3 x32 平台上并通过 FIFO 管道与 GUI 通信。
该消息是一种用 C 语言创建的结构,并在一个单一的 .h 文件中定义,项目中的所有各方都可以全局访问。
现在我的问题是,我设法创建了一个管道并向其写入数据,然后使用 "cat " 从终端读取管道的内容,但是当我尝试使用 pipe-handle 进行读取时dlang 我得到了很多 ?-chars 和 return 信息,这些信息表明 -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);
}
}
找到问题了,瑞典的SBS。我正在从错误的地方读取 file/pipe 将读取中的文件处理程序更正为 freader 而不是 fd。还要添加 import core.stdc.errno; writeln(errno) 给了我 errorno 的输出。 http://virtsync.com/c-error-codes-include-errno 显示内容