我如何知道我的客户端 iOS 应用程序是否已连接到服务器?
我按下按钮连接到服务器(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于遵循连接方法的人来说,解决方案
是使用
-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
,如下所示:(
据我所知!)学分转到来自这篇文章的deksa(虽然我不知道谁是创建者) ,因为我已经在网上见过几次,包括这里o SO)。这段代码被我稍微修改了(pingButton,statusText),如果您想要原始代码,请转到前面提到的链接。
Apple 开发者网站 有一些信息对此也是如此。
就像我说过的,我在网上看到过一些类似这样的东西,但现在我明白,连接后发生的一切都是“自动”的;例如,如果服务器通过
read()
暂停,则case NSStreamEventHasSpaceAvailable:
将被自动调用,并且其中的所有代码都将运行。现在我认为这个问题已经得到解答。
The solution for people following the connection method:
is using the
-(void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent
like this:
(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()
, thecase NSStreamEventHasSpaceAvailable:
will be called automatically, and all the code in there will be run.Now I consider this question answered.
虽然您没有提供足够的信息,但我建议使用 ASIHTTPRequest HTTP,以及用于 TCP 和 UDP 的 AsyncSocket。如果建立了连接,就会触发回调方法,
我不得不说,我对 CFNetwork 的经验非常有限,但对我来说,好像你只是在测试流对象是否存在 (
if(readStream && writeStream)
)。快速浏览一下 CFNetwork 编程指南:使用读取流 告诉我,您必须使用以下命令打开它
CFReadStreamOpen()
,如果确实打开了流,该函数将返回一个布尔值。顺便说一句:
而不是
你可以写
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.BTW:
instead of
you just can write
statusText.text = @"Connected!! :)";