3
我目前正在尝试从我的iPhone发送Hello World到运行工作服务器的远程计算机(通过iPhone上的telnet进行测试)。iPhone流编程(CFStream)Hello World
这里是我的代码:
#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
对于client.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];
}
是否有人有任何想法发生了什么问题?