views:

364

answers:

1

I'm trying to create a simple IRC client, purely for fun and to try and learn a bit more about socket programming in objective C.

So I've looked at some sample code and I have no idea why I can't seem to send or receive message upon connecting. Firstly I connect to the server using the following code:

if (![urlString isEqualToString:@""]) {
    NSURL* url = [NSURL URLWithString:urlString];
    if(!url){
        [self writeToOutput:@"Invalid Url"];
        return;
    }

    [self writeToOutput:@"Attempting Connection\n"];

    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)[url host], 6667, &readStream, &writeStream);

    if(!readStream || !writeStream){
        [self writeToOutput:@"Connection Failed!\n"];
        return;
    }

    _inputStream = (NSInputStream *)readStream;
    _outputStream = (NSOutputStream *)writeStream;
    [_inputStream setDelegate:self];
    [_outputStream setDelegate:self];
    [_inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [_outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [_inputStream open];
    [_outputStream open];
    [self writeToOutput:@"Connection Successful!\n"];
}

I then use the following to read/write from the streams:

case NSStreamEventHasBytesAvailable:
        if (stream == _inputStream)
        {
            //read data
            uint8_t buffer[1024];
            int len;
            while ([_inputStream hasBytesAvailable])
            {
                len = [_inputStream read:buffer maxLength:1024];
                if (len > 0)
                {
                    NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                    if (output != nil)
                    {
                        [self writeToOutput:output];
                    }
                }
            }
        }
        break;

    case NSStreamEventHasSpaceAvailable:
        if (stream == _outputStream)
        {
                const uint8_t *rawString=(const uint8_t *)[message UTF8String];         
                int len;

                len = [_outputStream write:rawString maxLength:[message length]];

                [self writeToOutput:[NSString stringWithFormat:@"%@\n",message]];
            }
        }
        break;

When I get an NSStreamEventHasBytesAvailable event, no message is read (len == 0) and when I write to the output stream I get a "Bad File Descriptor" error.

The messages being sent/received are just strings, I've copied/pasted a lot of code from the apple developer site but I think I mostly understand what's going on and can't understand why it isn't giving me something!

Any ideas or am I just on the wrong page totally?

P.S. writeToOutput is to write to the output on screen, not to the output stream. Confusing naming, I should change that...

A: 

The solution was actually outlined here, using the AsyncSocket class link AsyncSocket

However if anyone could tell me what was wrong with the above that would be great. As far as I'm aware, the Async socket class is just a wrapper for the functionality I have above isn't it?

Jonathan Snape