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

So, I'm looking for an eloquent way to change the [window setString:output] method. This is the .m file for a program that I am trying to write in order to play a MUD (a text based game). The inputstream represents a connection to the game server which sends my client information (bytes). The bytes are converted to strings everytime the hasBytesAvailable event occurs. These bytes are then displayed on the window's text field. The problem I am having is that each time the event occurs it replaces what was initially on the screen. I tried to create a placeholder string that held the output data then append onto that string to display. It does not seem to work the way I want it. I would like to be able to constantly get data from the stream which will display line by line continuously throughout the program. I've tried NSArray and NSMutableString... I can't seem to figure out how to create the global string variable that I can just append every time the hasBytesAvailable event occurs. Thank you all for reading and I hope you can help.

  • Well if this is the case then create singleton class insode that declare your mutablestring and just append inisde that and set into the screen – Hussain Shabbir Oct 27 '13 at 07:06
  • That worked flawlessly! Excuse me, as I've only been learning to program for a few weeks now. – Andy Francis Oct 28 '13 at 01:50
  • The string that I am receiving from the socket server is tagged in HTML, what would be a good way to create a class to strip the HTML tags out and set functions for the actual tags such as etc... – Andy Francis Oct 28 '13 at 01:51

1 Answers1

0

To answer your question on parsing HTML tags. Look into NSXMLParser and NSXMLParserDelegate to parse them yourself. Else take a look at HTML parsing options

lead_the_zeppelin
  • 2,017
  • 13
  • 23