Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > NSPipe in Cocoa

NSPipe in Cocoa
Thread Tools
Fresh-Faced Recruit
Join Date: Jan 2001
Status: Offline
Reply With Quote
Jan 12, 2001, 09:25 AM
 
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
     
monty  (op)
Fresh-Faced Recruit
Join Date: Jan 2001
Status: Offline
Reply With Quote
Jan 13, 2001, 06:48 AM
 
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
Guest
Status:
Reply With Quote
Jan 13, 2001, 11:48 AM
 
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
Reply With Quote
Jan 20, 2001, 08:20 AM
 
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
Reply With Quote
Jan 20, 2001, 01:08 PM
 
[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).]
     
monty  (op)
Fresh-Faced Recruit
Join Date: Jan 2001
Status: Offline
Reply With Quote
Jan 25, 2001, 07:18 AM
 
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
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:25 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2