- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我注意到在我的程序中使用 CFStreamCreatePairWithSocketToHost 时出现随机 EXC_BADACCESS 错误,但仅限于 ios6 - 当使用 ios5 模拟器时(即使在 ios6sdk 和 xcode4.5 中)一切正常。我最终将问题提取到随后的一个小测试程序中——如果你用它启用 guard malloc,它会立即爆炸(见下面的崩溃)。这也发生在弧形和非弧形情况下。
@interface PHAppDelegate : UIResponder <UIApplicationDelegate, NSStreamDelegate>
{
NSOutputStream* mOutputStream;
NSInputStream* mInputStream;
}
@implementation PHAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString* testAddress = @"192.168.1.0";
[self openWithHost:testAddress port:444];
return YES;
}
- (void)openWithHost:(NSString*)host port:(int)port
{
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault,
(CFStringRef)host, /*ip_addr*/
port,
&readStream,
&writeStream);
mInputStream = (NSInputStream *)readStream;
mOutputStream = (NSOutputStream *)writeStream;
if (mInputStream == nil)
{
NSLog(@"couldn't create the inputStream using CFStreamCreatePairWithSocketsToHost()");
return;
}
if (mOutputStream == nil)
{
NSLog(@"couldn't create the outputstream using CFStreamCreatePairWithSocketsToHost()");
return;
}
[mInputStream setDelegate:self];
[mOutputStream setDelegate:self];
[mInputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[mOutputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[mInputStream open];
[mOutputStream open];
}
#pragma mark NSStream delegate methods
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode
{
NSLog(@"stream-handleEvent");
}
@end
com.apple.networking.connection 线程崩溃:
#0 0x04b35140 in tcp_connection_destination_prepare_complete ()
#1 0x04b34fee in tcp_connection_destination_start ()
#2 0x04b34c2b in tcp_connection_start_next_destination ()
#3 0x04b33c70 in tcp_connection_handle_reachability_changed ()
#4 0x04b30a95 in __tcp_connection_start_block_invoke_0 ()
#5 0x049fa53f in _dispatch_call_block_and_release ()
#6 0x04a0c014 in _dispatch_client_callout ()
#7 0x049fc418 in _dispatch_queue_drain ()
#8 0x049fc2a6 in _dispatch_queue_invoke ()
#9 0x049fd280 in _dispatch_root_queue_drain ()
#10 0x049fd450 in _dispatch_worker_thread2 ()
#11 0x94e7de12 in _pthread_wqthread ()
#12 0x94e65cca in start_wqthread ()
EXC_BADACCESS @ address 0x04b35140
0x04b3513b <+0072> call 0x4b332de <tcp_connection_destination_list_remove>
0x04b35140 <+0077> mov 0x28(%esi),%eax
0x04b35143 <+0080> test %eax,%eax
最佳答案
我有非常相似的代码,适用于 iOS 5.x 和 iOS 6.x。唯一的区别是我在调用 CFStreamCreatePairWithSocketToHost
之前将 CFReadStreamRef
和 CFWriteStreamRef
初始化为 NULL
并且我传递了 NULL
分配器。为了方便起见,我通常还会将代码添加为 NSStream 上的一个类别。所以,代码看起来像这样:
+ (void)createStreamsToHostNamed:(NSString*)hostName port:(NSInteger)port inputStream:(NSInputStream* __autoreleasing *)inputStream outputStream:(NSOutputStream* __autoreleasing *)outputStream {
CFReadStreamRef readStream = NULL;
CFWriteStreamRef writeStream = NULL;
// Create a pair of of streams for a socket to the host specified
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)hostName, port, &readStream, &writeStream);
// Assign the output parameters
*inputStream = (__bridge_transfer NSInputStream*)readStream;
*outputStream = (__bridge_transfer NSOutputStream*)writeStream;
}
您可以使用如下代码调用该方法:
@interface SomeClass : NSObject <NSStreamDelegate>
@end
@implementation SomeClass {
NSInputStream* _inputStream;
NSOutputStream* _outputStream;
}
- (void)_setupMethod {
__autoreleasing NSInputStream* autoreleasingInputStream = nil;
__autoreleasing NSOutputStream* autoreleasingOutputStream = nil;
[NSStream createStreamsToHostNamed:kHostConstant port:kPortConstant inputStream:&autoreleasingInputStream outputStream:&autoreleasingOutputStream];
if(autoreleasingInputStream != nil && autoreleasingOutputStream != nil) {
_inputStream = autoreleasingInputStream;
[_inputStream setDelegate:self];
[_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
_outputStream = autoreleasingOutputStream;
[_outputStream setDelegate:self];
[_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[_inputStream open];
[_outputStream open];
}
}
// NSStreamDelegate methods ...
@end
关于ios - CFStreamCreatePairWithSocketToHost 用 ios6 模拟器坏了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12667852/
我是 iOS/MacOS 编程的新手,还没有找到任何我能理解的例子。我想只需几行代码就可以做到这一点,很抱歉,如果这已经涵盖了,但我无法找到它。 我正在使用 CFStreamCreatePairWit
我正在尝试通过 TCP 将 iOS 设备连接到服务器。我目前正在使用函数 CFStreamCreatePairWithSocketToHost。我的问题是我想设置 iOS 设备需要使用的本地端口。有没
有人可以告诉我 CFStreamCreatePairWithSocketToCFHost 和 CFStreamCreatePairWithSocketToHost 调用之间的区别吗? 还有什么是 CF
用例 我正在使用套接字通过 CFStreamCreatePairWithSocketToHost() 发送和接收数据,并且我试图了解在发送多个集数据时如何完成此操作(即不只是 1 个请求)。 问题 目
我正在使用 CFStreamCreatePairWithSocketToHost 创建一个套接字 tcp 连接来获取写入流(我不想准备任何数据): CFStreamCreatePairWithSock
我使用 NSInputstream 和 NSOutputstream 来建立连接并发送数据。我的流对象具有打开和关闭流的功能。我使用以下代码: @interface Stream() { NS
我正在尝试与我的服务器建立 TLS 连接,但在 CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, str, 5555, &readStre
我注意到在我的程序中使用 CFStreamCreatePairWithSocketToHost 时出现随机 EXC_BADACCESS 错误,但仅限于 ios6 - 当使用 ios5 模拟器时(即使在
终于可以在这里问我的第一个问题了! 预先:Xcode 7.1.1、OS 10.11.2、iOS 9.0.2(在物理设备上) 我正在编写一个与我的 Raspberry Pi 通信的小应用程序。我有一些从
我面临使用 CFStreamCreatePairWithSocketToHost 创建与 iPV6 的套接字连接的问题。但是我能够为相同的端口号创建与 IPV4 的套接字连接。 尝试了所有场景,例如添
我是 swift 编程的新手。我正在尝试从我的 ios 应用程序连接到服务器套接字。我有一个 View Controller ,在一个按钮操作中,我调用了连接到套接字服务器的函数 let socket
CFStream 类是否符合 IPV6 标准?我正在使用 CFStreamCreatePairWithSocketToHost 连接到套接字而不是低级套接字 api CFStreamCreatePai
我是一名优秀的程序员,十分优秀!