开发者

Reading from socket using SmallSockets makes my app crash

开发者 https://www.devze.com 2022-12-09 14:25 出处:网络
I\'m creating an app using the SmallSockets library ( http://smallsockets.sourceforge.net/ ). I have this code:

I'm creating an app using the SmallSockets library ( http://smallsockets.sourceforge.net/ ).

I have this code:

#import "MAController.h"
#import "Socket.h"


@implementation MAController

- (IBAction)doRequest:(id)sender
{
    //Initialize
    NSURL      *uUrl  = [[NSURL alloc] initWithString:[tfHost stringValue]];
    int         iPort = [[uUrl port] intValue];
    NSString   *sHost = [uUrl host];

    //Check
    if(sHost == NULL)
    {
        //Invalid Host name
        NSLog(@"Invalid Host name");
        NSAlert *aInvalidHost = [[[NSAlert alloc] init] autorelease];
        [aInvalidHost addButtonWithTitle:@"Ok"];
        [aInvalidHost setInformativeText:@"Invalid Host name"];
        [aInvalidHost setMessageText:@"You have entered an invalid host name. Please enter a valid host name."];
        [aInvalidHost setAlertStyle:NSInformationalAlertStyle];
        [aInvalidHost beginSheetModalForWindow:wMain modalDelegate:self didEndSelector:NULL contextInfo:nil];
        return;
    }
    if(iPort == 0)
    {
        iPort = 80;
    }

    //Create request
    NSString *sRequest = [[NSString alloc] initWithString:[tvRequest string]];

    //Create socket
    Socket *sSocket = [Socket socket];

    NSLog(@"Connecting...");
    [sSocket connectToHostName:sHost port:iPort];

    //Write request
    NSLog(@"Writing...");
    [sSocket writeString:sRequest];

    //Read response
    NSLog(@"Reading...");
    NSMutableData *mdResponse = [[[NSMutableData alloc] init] autorelease];

//IT CRASHES RIGHT HERE
    while([sSocket readData:mdResponse])
    {
        NSLog(@"Read.");
    }

    //Display response
    NSLog(@"Displaying...");
    NSString *sResponse = [[[NSString alloc] initWithData:mdResponse
                                             encoding:[NSString defaultCStringEncoding]]
                                             autorelease];
    [tvResponse setString:sResponse];

    [mdResponse release];
    [sSocket release];
}

@end

and here is what my debugger says after I click the button which fires the doRequest action:

2009-10-24 18:22:19.197 iDebug HTTP[4836:a0f] Connecting...

2009-10-24 18:22:19.399 iDebug HTTP[4836:a0f] Writing...

2009-10-24 18:22开发者_高级运维:19.400 iDebug HTTP[4836:a0f] Reading...

Hostname is http://www.example.com

and port is 80

can anyone explain me why my app hangs while its trying to read fromt the socket? Thanks in advance.

I have no firewall installed.


Because you're not running the event loop; you're just reading from a socket. You don't show what the text of tvRequest is, but my guess is that the server is waiting for further input while you're waiting for output from it.

You may find NSURLConnection easier to work with. It's asynchronous, which makes playing nice with the event loop easier, and the request and response classes are designed specifically to make HTTP easy.

0

精彩评论

暂无评论...
验证码 换一张
取 消