 |
 |
NSPipe in Cocoa
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2001
Status:
Offline
|
|
Help,
I'm working on a shell wrapper class so that i can create GUI interfaces for the many handy unix commands. I know how to start up a Unix program using NSTask and I can divert the standard input and output to NSPipes. The problem is for the program not to hang I have to close the output pipe's FileHandle before I read the response on the input pipe. This works but it means I can only send one command and get one response per session because i've closed the output pipe and can't write to it again. I've downloaded sample code but the commands they are implementing only require one input and output so it doesn't affect them. What have I missed? or should I be using another class to do this?
peter
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2001
Status:
Offline
|
|
Ok I think I half answered my own question. The pipe is only meant for a single input/output ie you can't double pipe in unix. My second question though is: Is there another class I can use to communicate with a unix process by controling its standard input/output for the whole amount of time that the program is running ie. start the background process when my program starts and quit it when mine does.
peter
|
|
|
| |
|
|
|
 |
|
 |
|
scruffy
|
|
probably a unix domain socket would do the trick. Sockets are two-way, so you would only need one for both directions.
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Feb 2000
Status:
Offline
|
|
Unix tools are generally one shot afairs...
basically, you'd start the task connected to the input and output pipes
then pump everything into the input pipe then close the input pipe
and read everything from the output pipe
and then close that pipe I suppose (or it'll get closed for you)
and then quit...
of ocurse, you might want to stay running,
in which case you start the tool again...
one shot.
(well I haven't checked any of this and am in fact making it up  )
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Dec 2000
Status:
Offline
|
|
[QUOTE]Is there another class I can use to communicate with a unix process by controling its standard input/output for the whole amount of time that the program is running[\QUOTE]
You're already doing that. Just don't throw out you pipes. Here:
Code:
//allocate and initialize the objects
NSPipe *inputPipe = [[NSPipe alloc] init];
NSPipe *outputPipe = [[NSPipe alloc] init];
NSTask *unixTask = [[NSTask alloc] init];
NSFileHandle *writeHandle = [inputPipe fileHandleForWriting];
NSFileHandle *readHandle = [outputPipe fileHandleForReading];
//set up the task
[unixTask setStandardInput:inputPipe];
[unixTask setStandardOutput :outputPipe];
[unixTask setStandardError :outputPipe];
[unixTask setLaunchPath:/*path to the executable*/];
[unixTask setArguments:/*arguments*/];
[unixTask launch];
Now you can write data to the write handle or read data from the read handle. You'll need to do some checks to make sure the task is still running and whatnot, but that shouldn't be a problem.
When you're done with it, you can either waitUntilExit on it, or there's some function to kill the task.
[This message has been edited by Mniot (edited 01-20-2001).]
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2001
Status:
Offline
|
|
Mniot,
I tried that before I posted the question. The trouble is that although you can read multiple times, the write is terminated by closing the pipe. It's not just a newline like it is in a shell. And you can't set the standard input while an app is running so you can't reset it.
Scruffy,
Does the other program have to support the socket or can you simply redirect Standard Input/Output to the socket? I'm really looking for a way to write guis for Unix utilities. But thinking about, starting the program for each new command really isn't that bad. That way it's not hogging resources.
peter
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |