gpt4 book ai didi

iPhone流式编程(CFStream) Hello World

转载 作者:搜寻专家 更新时间:2023-10-30 20:27:33 26 4
gpt4 key购买 nike

我目前正在尝试将 Hello World 从我的 iPhone 发送到运行正常服务器的远程计算机(通过 iPhone 上的 telnet 测试)。

这是我的代码:

#import "client.h"

@implementation client

- (client*) client:init {
self = [super init];
[self connect];
return self;
}

- (void)connect {
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[NSString stringWithFormat: @"192.168.1.1"], 50007, NULL, &writeStream);
NSLog(@"Creating and opening NSOutputStream...");
oStream = (NSOutputStream *)writeStream;
[oStream setDelegate:self];
[oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[oStream open];
}

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
NSLog(@"stream:handleEvent: is invoked...");

switch(eventCode) {
case NSStreamEventHasSpaceAvailable:
{
if (stream == oStream) {
NSString * str = [NSString stringWithFormat: @"Hello World"];
const uint8_t * rawstring =
(const uint8_t *)[str UTF8String];
[oStream write:rawstring maxLength:strlen(rawstring)];
[oStream close];
}
break;
}
}
}

@end

对于客户端.h:

#import <UIKit/UIKit.h>


@interface client : NSObject {
NSOutputStream *oStream;
}

-(void)connect;

@end

最后,在 AppDelegate.m 中:

- (void)applicationDidFinishLaunching:(UIApplication *)application {    

// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[client new];
}

有人知道出了什么问题吗?

最佳答案

您的初始格式不正确。您创建了一个名为 client: 的方法,而不是 init,它采用一个名为 初始化。由于永远不会调用此方法(客户端),因此您的客户端永远不会连接。相反,将该方法替换为以下内容:

- (id)init
{
if( (self = [super init]) ) {
[self connect];
}
return self;
}

现在,当您调用[Client new] 时,您的客户端实际上将被初始化并在其自身上调用connect。我还对其进行了轻微的重组,使其遵循常见的 Objective-C/Cocoa 初始化模式。

关于iPhone流式编程(CFStream) Hello World,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2601950/

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