Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 使用NSTask时处理错误_Objective C_Apache_Nstask - Fatal编程技术网

Objective c 使用NSTask时处理错误

Objective c 使用NSTask时处理错误,objective-c,apache,nstask,Objective C,Apache,Nstask,作为一个学习项目,我正在为Apache压力测试命令行工具“ab”编写一个简单的gui。它需要一个完整的URL,包括index.html或simular等文件名,作为其参数之一。如果未指定文件名,“ab”将回显“无效url”,并显示可用标志的列表 我希望捕获此“错误”,并尝试使用NSTASK standarderror输出。真的不能让它工作。这是否会被归类为一种会导致标准错误的错误 除了在启动NSTask之前验证URL输入之外,您认为我可以防止或更确切地说捕获此错误吗 我的简单代码: - (voi

作为一个学习项目,我正在为Apache压力测试命令行工具“ab”编写一个简单的gui。它需要一个完整的URL,包括index.html或simular等文件名,作为其参数之一。如果未指定文件名,“ab”将回显“无效url”,并显示可用标志的列表

我希望捕获此“错误”,并尝试使用NSTASK standarderror输出。真的不能让它工作。这是否会被归类为一种会导致标准错误的错误

除了在启动NSTask之前验证URL输入之外,您认为我可以防止或更确切地说捕获此错误吗

我的简单代码:

- (void) stressTest:(NSString *)url withNumberOfRequests:(int)requests sendSimultaneously:(int)connections {

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSString *abPath = [[mainBundle bundlePath] stringByAppendingString:@"/Contents/Resources/ab"];

    NSString* requestsStr = [NSString stringWithFormat:@"%i", requests];
    NSString* connectionsStr = [NSString stringWithFormat:@"%i", connections];

    // Init objects for tasks and pipe
    NSTask *abCmd = [NSTask new];
    NSPipe *outputPipe = [NSPipe pipe];
    [abCmd setLaunchPath:abPath];
    [abCmd setArguments:[NSArray arrayWithObjects:@"-n", requestsStr, @"-c", connectionsStr, url, nil]];
    [abCmd setStandardOutput:outputPipe];
    [abCmd launch];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readCompleted:) name:NSFileHandleReadToEndOfFileCompletionNotification object:[outputPipe fileHandleForReading]];
    [[outputPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
}

- (void)readCompleted:(NSNotification *)notification {

    NSString * tempString = [[NSString alloc] initWithData:[[notification userInfo] objectForKey:NSFileHandleNotificationDataItem] encoding:NSASCIIStringEncoding];
   [resultTextOutlet setString:tempString];
   [[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:[notification object]];
}

ab
将其错误消息(包括使用信息)写入标准错误。您当前只读取标准输出。要访问错误消息或使用信息,您需要分配第二个
NSPipe
,将其传递到
-[NSTask setStandardError:
,然后从中读取数据。

您能调试并告诉我们错误的确切位置吗。我想这是在您设置启动路径的时候,但是可以肯定的是,错误发生在我使用NSTask时。它可以在OSX终端中通过键入ab-C1-N1来重复htp://www.example.com. Ab需要URL中的文件名。我甚至不确定它是否会被归类为可以通过管道传输的“错误”?你可以通过管道传输,但你需要从
main()
这条好消息中获取它,但我对这一点很陌生,才能理解这意味着什么,对不起!我的应用程序中的主循环还是NSApplicationMain中的主循环?如果您有时间详细说明,我将不胜感激。然而,如果这是非常复杂的,不要洗澡。对我来说,这不是一件难事,我只是想学习。我目前通过在启动NSEvent之前严格验证URL来避免这个问题。你是对的!AB确实给standardError写信,是我读错了。在上面的代码中,添加另一个侦听-setStandardError的管道,并将“nil”而不是管道对象作为参数“object:”放入通知中心。现在一切正常。谢谢