I have an application which works with sockets and reads / writes data. It uses Foundation framework combined with CFNetwork and stdio.
Here is the issue - when it is launched from console (bash shell) it works 100% fine and there is nothing wrong with. However when it is invoked by another application via NSTask madness begins. The whole application goes insane and it only reads the socket once and then hangs up (it is meant to exit after it is done).
This application does not rely on environmental variables or any other things like that. It is not a user issue either. When it is launched it sends a simple request to 开发者_JS百科the server and 'printf's the response and reads again. This happens untill a termination packet is recieved.
I am really confused, and it feels like there is something inside the framework which makes the app insane just to piss the programmer off.
By the way, I'm on Mac OS X Snow Leopard and the application is for the same platform.
EDIT 1 : Redirecting stdout to an NSPipe causes it. But why ?
libc
treats a pipe/file and a console connected to a (pseudo) terminal differently. In particular, the default buffering policy is different. See the extensive discussion in this Stack Overflow Q&A.
So, it's perfectly conceivable that a program which works when connected to a (pseudo) terminal won't work with a pipe. If you need more specific advice, you need to post (at least the skeleton of) your code.
精彩评论