0
#import "AppDelegate.h"





@implementation AppDelegate
@synthesize inputStream;
@synthesize outputStream;
@synthesize textField;
@synthesize window;
@synthesize aText;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self initNetworkCommunication];

}

- (void)initNetworkCommunication {
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"tec.skotos.net", 6730, &readStream, &writeStream);
    inputStream = (__bridge NSInputStream *)readStream;
    outputStream = (__bridge NSOutputStream *)writeStream;
    [inputStream setDelegate:self];
    [outputStream setDelegate:self];
    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [inputStream open];
    [outputStream open];
    NSString *response = [NSString stringWithFormat:@"/\\/connect: n/a!!n/a"];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    NSString *end = @"\n";
    NSData *endData = [[NSData alloc] initWithData:[end dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
    [outputStream write:[endData bytes] maxLength:[endData length]];



}



- (IBAction)sendMessage:(id)sender {
    NSString *response = [textField stringValue ];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    NSString *end = @"\n";
    NSData *endData = [[NSData alloc] initWithData:[end dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];
    [outputStream write:[endData bytes] maxLength:[endData length]];
}


- (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {

    switch (streamEvent) {

        case NSStreamEventOpenCompleted:
            NSLog(@"Stream opened");
            break;
        case NSStreamEventHasSpaceAvailable:
            NSLog(@"Has space");
            break;

        case NSStreamEventHasBytesAvailable:

            if (theStream == inputStream) {
                uint8_t buffer[900000];
                long len;

                while ([inputStream hasBytesAvailable]) {
                    len = [inputStream read:buffer maxLength:sizeof(buffer)];
                    if (len > 0) {

                        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];

                        NSLog(@"Has Bytes");
                        if (nil != output) {
                            NSLog(@"%@", output);
                            [window setString:output];



                        }
                    }
                }
            }
            break;

        case NSStreamEventEndEncountered:
            NSLog(@"Error.");
            break;

    }

}

@end

所以,我正在寻找一种雄辩的方式来改变 [window setString:output] 方法。这是我为玩 MUD(基于文本的游戏)而尝试编写的程序的 .m 文件。输入流表示与发送我的客户端信息(字节)的游戏服务器的连接。每次发生 hasBytesAvailable 事件时,字节都会转换为字符串。然后这些字节显示在窗口的文本字段中。我遇到的问题是每次事件发生时它都会替换最初在屏幕上的内容。我尝试创建一个占位符字符串来保存输出数据,然后附加到该字符串以显示。它似乎没有按我想要的方式工作。我希望能够不断地从流中获取数据,这些数据将在整个程序中连续逐行显示。我' 我尝试过 NSArray 和 NSMutableString ...我似乎无法弄清楚如何创建全局字符串变量,每次发生 hasBytesAvailable 事件时我都可以附加该变量。谢谢大家的阅读,希望对大家有所帮助。

4

1 回答 1

0

回答关于解析 HTML 标签的问题。查看NSXMLParserNSXMLParserDelegate自己解析它们。否则看看HTML 解析选项

于 2013-11-03T23:32:10.380 回答