Haskell <-> C/C++ comunication (sockets/pipes?)

Hi all, I was thinking about making some GUIs in Qt/KDE for some haskell applications (compiled with ghc). My first idea was to simply run haskell applications with some args using the GUI and get the results from some file or something like that. Anyway someone told me that a better idea would be to use pipes to enable the GUI to comunicate with the application. I checked the ghc libs and there was a pipe function, but I would need named pipes here (right?) since the two processes are not related. I didn't found any function for that in the ghc libs... is there one? I was also told that another (better way) to do this would be to use sockets. I've worked a little bit with posix stuff (fork, pipes, etc) but I never worked with sockets. I was thinking about using named pipes if that's possible, anyway, Id like to know what you think. Is possible. Is sockets a good solution? better? worth the trouble of learning how to use them? Any better ideas? J.A. PS: Good luck to the GUI task force ;)

Jorge Adriano wrote:
Hi all, I was thinking about making some GUIs in Qt/KDE for some haskell applications (compiled with ghc). My first idea was to simply run haskell applications with some args using the GUI and get the results from some file or something like that. Anyway someone told me that a better idea would be to use pipes to enable the GUI to comunicate with the application. I checked the ghc libs and there was a pipe function, but I would need named pipes here (right?) since the two processes are not related. I didn't found any function for that in the ghc libs... is there one?
I was also told that another (better way) to do this would be to use sockets. I've worked a little bit with posix stuff (fork, pipes, etc) but I never worked with sockets.
I was thinking about using named pipes if that's possible, anyway, Id like to know what you think. Is possible. Is sockets a good solution? better? worth the trouble of learning how to use them? Any better ideas?
J.A.
PS: Good luck to the GUI task force ;)
Actually I used unnamed pipes to make it work in Windows, since I needed it to work both on Unix and Windows. You basically rename the stdin and stdout of the haskell application into pipes so that the host app can talk to the stdin of the haskell app, and listen to the stdout of the haskell app. A tip: under Windows, don't use the "posix functions" Windows provides if you code your app in C/C++: you'll run out of file handles before you've finished renaming stdin/stdout. Instead use the Windows functions. Sengan

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi Jorge, This is exa from #kde :) Using stdin/stdout with ASCII first would be certainly easier. All you have to do is to define some GUI commands. Then if you convert to some binary interface, you could again use either pipes or sockets. I'd referred to this function on irc, but anyway. In posix category. createNamedPipe :: FilePath -> FileMode -> IO () createNamedPipe fifo mode calls mkfifo to create a new named pipe, fifo, with permissions based on mode. And of course you could create that fifo externally with a shell script if you'd like and run stuff from there. I'd almost certainly make it run over stdin/stdout on second thought. You do the connection externally as Sengan says. Create the pipes. fork the program. connect the stdin/stdout of child to your pipes. I think he did it writing C code, but you could also do it using bash, right? #! /bin/sh mkfifo input mkfifo output app <input >output & guifrontend >input

Actually I used unnamed pipes to make it work in Windows, since I needed it to work both on Unix and Windows. You basically rename the stdin and stdout of the haskell application into pipes so that the host app can talk to the stdin of the haskell app, and listen to the stdout of the haskell app. A
Nice idea :) So you called the haskell app from C using popen... right? ( I haven't worked with this stuff for ages, and I never really liked it that much, by that time that was just some stuff I had to do in C when I could be playing with my haskell book :) I'm thinking about doing the other way around. Calling the gui from the haskell app renaming the stdin and stdout of the gui into pipes. Seems to me like the function "runProcess" can do the trick. That way I could do $haskellapp And run the text based mode or $haskellapp --with gui and the haskell app would call the GUI.
tip: under Windows, don't use the "posix functions" Windows provides if you code your app in C/C++: you'll run out of file handles before you've finished renaming stdin/stdout. Instead use the Windows functions.
Sengan
I'm not really into windows... :) Anyway, thanks for the tip. J.A.

Hi Jorge,
This is exa from #kde :)
Hi again :)
Using stdin/stdout with ASCII first would be certainly easier. All you have to do is to define some GUI commands. Then if you convert to some binary interface, you could again use either pipes or sockets.
Yes, I'll start with simple stdin/stdout first.
I'd referred to this function on irc, but anyway. In posix category.
createNamedPipe :: FilePath -> FileMode -> IO ()
Yeap, you did, and I thought it was so easy to remember that I didn't copy/paste... :) Then I forgot and couldn't find it in the Posix "input and output" section, so I wondered if you had actually told me about createPipe (it was in the file and directories section)
createNamedPipe fifo mode calls mkfifo to create a new named pipe, fifo, with permissions based on mode.
And of course you could create that fifo externally with a shell script if you'd like and run stuff from there. I'd almost certainly make it run over stdin/stdout on second thought. You do the connection externally as Sengan says. Create the pipes. fork the program. connect the stdin/stdout of child to your pipes. I think he did it writing C code, but you could also do it using bash, right?
#! /bin/sh mkfifo input mkfifo output app <input >output & guifrontend >input
mmm it is a nice idea too :) anyway I don't know if using the same output for both, interactive shell and gui, is the best option, I'll have to give it some more thought.
Since you will be using KDE, you don't have to worry too much about Windows. Unless of course one day you want to port it. :) Then you might consider writing another such GUI using MFC with Visual C++. And make it an ActiveX component, get it signed by MS, etc, etc. Or, alternatively port a large portion of KDE to windows.
Regards,
I think I'll stick to Linux and maybe other *nixes for now :) Thanks for all the ideas, J.A.
participants (3)
-
Eray Ozkural
-
Jorge Adriano
-
Sengan