gpt4 book ai didi

objective-c - Cocoa 中打开的读取流出现 Posix 错误 14(错误地址)。提示?

转载 作者:行者123 更新时间:2023-12-03 17:08:45 25 4
gpt4 key购买 nike

我正在遵循CFNetwork编程指南中的示例代码,特别是使用流时防止阻塞部分。我的代码与他们的代码几乎相同(如下),但是,当我连接到我的服务器时,我收到 posix 错误 14(地址错误 - 是那个错误的 IP 地址吗(除非不是)?我进行的某些调用的内存地址错误?或者什么?!.

我不知道如何调试这个。我对整个 CFNetworking 事情真的很陌生,而且一开始就不是网络方面的专家(我真正喜欢 Java 的一件事:简单的网络!:D)

无论如何,日志如下,代码如下。任何提示将不胜感激。

日志:

[6824:20b] [DEBUG] Compat version: 30000011
[6824:20b] [DEBUG] resovled host.
[6824:20b] [DEBUG] writestream opened.
[6824:20b] [DEBUG] readstream client assigned.
[6824:20b] [DEBUG] readstream opened.
[6824:20b] [DEBUG] *** Read stream reported kCFStreamEventErrorOccurred
[6824:20b] [DEBUG] *** POSIX error: 14 - Bad address
[6824:20b] Error closing readstream
[6824:20b] [DEBUG] Writing int: 0x09000000 (0x00000009)

代码:

+ (BOOL) connectToServerNamed:(NSString*)name atPort:(int)port {
CFHostRef theHost = CFHostCreateWithName (NULL, (CFStringRef)name);
CFStreamError error;

if (CFHostStartInfoResolution (theHost, kCFHostReachability, &error))
{
NSLog (@"[DEBUG] resovled host.");
CFStreamCreatePairWithSocketToCFHost (NULL, theHost, port, &readStream, &writeStream);
if (CFWriteStreamOpen(writeStream))
{
NSLog (@"[DEBUG] writestream opened.");

CFStreamClientContext myContext = { 0, self, NULL, NULL, NULL };
CFOptionFlags registeredEvents = kCFStreamEventHasBytesAvailable |
kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered;
if (CFReadStreamSetClient (readStream, registeredEvents, readCallBack, &myContext))
{
NSLog (@"[DEBUG] readstream client assigned.");
CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(),
kCFRunLoopCommonModes);
if (CFReadStreamOpen(readStream))
{
NSLog (@"[DEBUG] readstream opened.");
CFRunLoopRun();
// Lots of error condition handling snipped.
[...]
return YES;
}


void readCallBack (CFReadStreamRef stream, CFStreamEventType event, void *myPtr)
{
switch (event)
{
case kCFStreamEventHasBytesAvailable:
{
CFIndex bytesRead = CFReadStreamRead(stream, buffer, kNetworkyBitsBufferSize); // won't block
if (bytesRead > 0) // <= 0 leads to additional events
{
if (listener)
{
UInt8 *tmpBuffer = malloc (sizeof (UInt8) * bytesRead);
memcpy (buffer, tmpBuffer, bytesRead);
NSLog(@"[DEBUG] reveived %d bytes", bytesRead);
[listener networkDataArrived:tmpBuffer count:bytesRead];
}
NSLog(@"[DEBUG] reveived %d bytes; no listener", bytesRead);
}
}
break;

case kCFStreamEventErrorOccurred:
NSLog(@"[DEBUG] *** Read stream reported kCFStreamEventErrorOccurred");
CFStreamError error = CFReadStreamGetError(stream);
logError(error);
[NetworkyBits shutDownRead];
break;

case kCFStreamEventEndEncountered:
NSLog(@"[DEBUG] *** Read stream reported kCFStreamEventEndEncountered");
[NetworkyBits shutDownRead];
break;
}
}

void logError (CFStreamError error)
{
if (error.domain == kCFStreamErrorDomainPOSIX) // Interpret error.error as a UNIX errno.
{
NSLog (@"[DEBUG] *** POSIX error: %d - %s", (int) error.error, strerror(error.error));
}
else if (error.domain == kCFStreamErrorDomainMacOSStatus)
{
NSLog (@"[DEBUG] *** MacOS error: %d", (int) error.error);
}
else
{
NSLog (@"[DEBUG] *** Stream error domain: %d, error: %d", (int) error.error);
}
}

最佳答案

奥利,在哪里

buffer

您提供给

CFReadStreamRead()

从哪里来? EFAULT 是一个错误的缓冲区地址...您确定您实际上已初始化此缓冲区以指向有效的内容吗?这显然是全局性的或有时......这本身就是一个非常糟糕的主意。您应该在函数中分配它,或者它应该是一个 ivar(如果您使用的是 Obj-C)。

关于objective-c - Cocoa 中打开的读取流出现 Posix 错误 14(错误地址)。提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/325122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com