我如何知道我的客户端 iOS 应用程序是否已连接到服务器?

发布于 2024-11-15 16:03:36 字数 588 浏览 4 评论 0原文

我按下按钮连接到服务器(TCP),但我不知道它是否连接.. 这是代码的一部分:

[self connectToServerUsingCFStream:msg portNo:50000];

    if(readStream && writeStream)
    {
        NSString *newText = [[NSString alloc] initWithFormat:@"Connected!! :)"]; 
        statusText.text = newText;
        [newText release];
        pingButton.hidden = NO;
    }
    else
    {
        NSString *newText = [[NSString alloc] initWithFormat:@"Connection unsuccessful :("]; 
        statusText.text = newText;
        [newText release];
    }

即使服务器离线,我总是得到“已连接!!:)”:s

I press the button to connect to the server (TCP), but i don't know whether it connected or not..
Here is that part of the code:

[self connectToServerUsingCFStream:msg portNo:50000];

    if(readStream && writeStream)
    {
        NSString *newText = [[NSString alloc] initWithFormat:@"Connected!! :)"]; 
        statusText.text = newText;
        [newText release];
        pingButton.hidden = NO;
    }
    else
    {
        NSString *newText = [[NSString alloc] initWithFormat:@"Connection unsuccessful :("]; 
        statusText.text = newText;
        [newText release];
    }

I always get the "Connected!! :)" even if the server is offline :s

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分分钟 2024-11-22 16:03:36

对于遵循连接方法的人来说,解决方案

CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, 
                                   (CFStringRef) urlStr, 
                                   portNo, 
                                   &readStream, 
                                   &writeStream);

if (readStream && writeStream) 
{
    CFReadStreamSetProperty(readStream, 
                            kCFStreamPropertyShouldCloseNativeSocket, 
                            kCFBooleanTrue);
    CFWriteStreamSetProperty(writeStream, 
                             kCFStreamPropertyShouldCloseNativeSocket, 
                             kCFBooleanTrue);

    iStream = (NSInputStream *)readStream;
    [iStream retain];
    [iStream setDelegate:self];
    [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] 
                       forMode:NSDefaultRunLoopMode];
    [iStream open];

    oStream = (NSOutputStream *)writeStream;
    [oStream retain];
    [oStream setDelegate:self];
    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] 
                       forMode:NSDefaultRunLoopMode];
    [oStream open];         
} 

是使用

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

如下所示:(

-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
NSString *io;
if (theStream == iStream) io = @">>";
else io = @"<<";

NSString *event;
switch (streamEvent)
{
    case NSStreamEventNone:
        event = @"NSStreamEventNone";
        statusText.text =  @"Can not connect to the host!";
        break;

    case NSStreamEventOpenCompleted:
        event = @"NSStreamEventOpenCompleted";
        pingButton.hidden = NO;
        statusText.text = @"Connected";
        break;

    case NSStreamEventHasBytesAvailable:
        event = @"NSStreamEventHasBytesAvailable";
        if (theStream == iStream)
        {
            //read data
            uint8_t buffer[1024];
            int len;
            while ([iStream hasBytesAvailable])
            {
                len = [iStream read:buffer maxLength:sizeof(buffer)];
                if (len > 0)
                {
                    NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                    NSData *theData = [[NSData alloc] initWithBytes:buffer length:len];
                    if (nil != output)
                    {
                        //do something with data
                    }
                }
            }
        }
        break;

    case NSStreamEventHasSpaceAvailable:
        event = @"NSStreamEventHasSpaceAvailable";
        if (theStream == oStream)
        {
            //send data
            uint8_t buffer[11] = "I send this";             
            int len;

            len = [oStream write:buffer maxLength:sizeof(buffer)];
            if (len > 0)
            {
                NSLog(@"Command send");
                [oStream close];
            }
        }

        break;

    case NSStreamEventErrorOccurred:
        event = @"NSStreamEventErrorOccurred";
        statusText.text = @"Can not connect to the host!";
        pingButton.hidden = YES;
        break;

    case NSStreamEventEndEncountered:
        event = @"NSStreamEventEndEncountered";
        statusText.text = @"Connection closed by the server.";
        pingButton.hidden = YES;
        [theStream close];
        [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [theStream release];
        theStream = nil;
        break;

    default:
        event = @"** Unknown";
}

NSLog(@"%@ : %@", io, event);
}

据我所知!)学分转到来自这篇文章deksa(虽然我不知道谁是创建者) ,因为我已经在网上见过几次,包括这里o SO)。这段代码被我稍微修改了(pingButton,statusText),如果您想要原始代码,请转到前面提到的链接。

Apple 开发者网站 有一些信息对此也是如此。

就像我说过的,我在网上看到过一些类似这样的东西,但现在我明白,连接后发生的一切都是“自动”的;例如,如果服务器通过read()暂停,则case NSStreamEventHasSpaceAvailable:将被自动调用,并且其中的所有代码都将运行。

现在我认为这个问题已经得到解答。

The solution for people following the connection method:

CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, 
                                   (CFStringRef) urlStr, 
                                   portNo, 
                                   &readStream, 
                                   &writeStream);

if (readStream && writeStream) 
{
    CFReadStreamSetProperty(readStream, 
                            kCFStreamPropertyShouldCloseNativeSocket, 
                            kCFBooleanTrue);
    CFWriteStreamSetProperty(writeStream, 
                             kCFStreamPropertyShouldCloseNativeSocket, 
                             kCFBooleanTrue);

    iStream = (NSInputStream *)readStream;
    [iStream retain];
    [iStream setDelegate:self];
    [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop] 
                       forMode:NSDefaultRunLoopMode];
    [iStream open];

    oStream = (NSOutputStream *)writeStream;
    [oStream retain];
    [oStream setDelegate:self];
    [oStream scheduleInRunLoop:[NSRunLoop currentRunLoop] 
                       forMode:NSDefaultRunLoopMode];
    [oStream open];         
} 

is using the

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

like this:

-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
{
NSString *io;
if (theStream == iStream) io = @">>";
else io = @"<<";

NSString *event;
switch (streamEvent)
{
    case NSStreamEventNone:
        event = @"NSStreamEventNone";
        statusText.text =  @"Can not connect to the host!";
        break;

    case NSStreamEventOpenCompleted:
        event = @"NSStreamEventOpenCompleted";
        pingButton.hidden = NO;
        statusText.text = @"Connected";
        break;

    case NSStreamEventHasBytesAvailable:
        event = @"NSStreamEventHasBytesAvailable";
        if (theStream == iStream)
        {
            //read data
            uint8_t buffer[1024];
            int len;
            while ([iStream hasBytesAvailable])
            {
                len = [iStream read:buffer maxLength:sizeof(buffer)];
                if (len > 0)
                {
                    NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding];
                    NSData *theData = [[NSData alloc] initWithBytes:buffer length:len];
                    if (nil != output)
                    {
                        //do something with data
                    }
                }
            }
        }
        break;

    case NSStreamEventHasSpaceAvailable:
        event = @"NSStreamEventHasSpaceAvailable";
        if (theStream == oStream)
        {
            //send data
            uint8_t buffer[11] = "I send this";             
            int len;

            len = [oStream write:buffer maxLength:sizeof(buffer)];
            if (len > 0)
            {
                NSLog(@"Command send");
                [oStream close];
            }
        }

        break;

    case NSStreamEventErrorOccurred:
        event = @"NSStreamEventErrorOccurred";
        statusText.text = @"Can not connect to the host!";
        pingButton.hidden = YES;
        break;

    case NSStreamEventEndEncountered:
        event = @"NSStreamEventEndEncountered";
        statusText.text = @"Connection closed by the server.";
        pingButton.hidden = YES;
        [theStream close];
        [theStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [theStream release];
        theStream = nil;
        break;

    default:
        event = @"** Unknown";
}

NSLog(@"%@ : %@", io, event);
}

(for all I know!) The credits go to deksa from this post (though i don't know who was the creator, because i've seen this some times on the web, including here o SO). This code was slightly modified by me (pingButton, statusText), if you want the original one go to the link previously mentioned.

The Apple Developer Site has some info on this as well.

Like i've said, I had seen some stuff looking like this on the web, but now i understand that everything that happens after you connect is "automatic"; for instance, if the server is on hold with a read(), the case NSStreamEventHasSpaceAvailable: will be called automatically, and all the code in there will be run.

Now I consider this question answered.

诠释孤独 2024-11-22 16:03:36

虽然您没有提供足够的信息,但我建议使用 ASIHTTPRequest HTTP,以及用于 TCP 和 UDP 的 AsyncSocket。如果建立了连接,就会触发回调方法,

我不得不说,我对 CFNetwork 的经验非常有限,但对我来说,好像你只是在测试流对象是否存在 (if(readStream && writeStream))。
快速浏览一下 CFNetwork 编程指南:使用读取流 告诉我,您必须使用以下命令打开它CFReadStreamOpen(),如果确实打开了流,该函数将返回一个布尔值。

if (!CFReadStreamOpen(myReadStream)) {
    CFStreamError myErr = CFReadStreamGetError(myReadStream);
    // An error has occurred.
        if (myErr.domain == kCFStreamErrorDomainPOSIX) {
        // Interpret myErr.error as a UNIX errno.
        } else if (myErr.domain == kCFStreamErrorDomainMacOSStatus) {
        // Interpret myErr.error as a MacOS error code.
            OSStatus macError = (OSStatus)myErr.error;
        // Check other error domains.
    }
}

顺便说一句:
而不是

NSString *newText = [[NSString alloc] initWithFormat:@"Connected!! :)"]; 
statusText.text = newText;
[newText release];

你可以写 statusText.text = @"Connected!! :)";

Although you did not provide enough informations, I'd suggest to use ASIHTTPRequest for HTTP, and AsyncSocket for TCP and UDP. If an connection was established, callback methods will be triggered,

I have to say, that my experiences with CFNetwork are very limited, but for me it seems, as if you are just testing, if stream objects exists (if(readStream && writeStream)).
A quick look at CFNetwork Programming Guide: Working with Read Streams tells me, that you have to open it with CFReadStreamOpen(), this function will return an boolean, if it really did open the stream.

if (!CFReadStreamOpen(myReadStream)) {
    CFStreamError myErr = CFReadStreamGetError(myReadStream);
    // An error has occurred.
        if (myErr.domain == kCFStreamErrorDomainPOSIX) {
        // Interpret myErr.error as a UNIX errno.
        } else if (myErr.domain == kCFStreamErrorDomainMacOSStatus) {
        // Interpret myErr.error as a MacOS error code.
            OSStatus macError = (OSStatus)myErr.error;
        // Check other error domains.
    }
}

BTW:
instead of

NSString *newText = [[NSString alloc] initWithFormat:@"Connected!! :)"]; 
statusText.text = newText;
[newText release];

you just can write statusText.text = @"Connected!! :)";

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文