xcode Objective-C 中的简单套接字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3403930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple Sockets in Objective-C
提问by hassaanm
I've had trouble finding simple guides/examples for basic sockets in Objective-C (using NSSocketPort with NSFileHandle or using CFSocket/CSNetwork). Can anyone recommend a guide or a useful example? I would appreciate this greatly! I have tried to use this, but it is incomplete. Thanks!
我在 Objective-C 中找不到基本套接字的简单指南/示例(使用 NSSocketPort 和 NSFileHandle 或使用 CFSocket/CSNetwork)时遇到了麻烦。谁能推荐一个指南或一个有用的例子?我将不胜感激!我曾尝试使用this,但它不完整。谢谢!
P.S. I have been stuck in this part of my project for awhile and am starting to get desperate for some help.
PS 我已经被困在我项目的这一部分一段时间了,并且开始急需一些帮助。
采纳答案by JosephH
The easiest way I've found to do this is the Cocoa Async Socket class:
我发现最简单的方法是 Cocoa Async Socket 类:
https://github.com/robbiehanson/CocoaAsyncSocket
https://github.com/robbiehanson/CocoaAsyncSocket
It's pretty straight forward to use, there's good html documentation included.
它使用起来非常简单,包含很好的 html 文档。
回答by Mininova Web
I know I am submitting answer to an very old question. In case you(visitor/stackoverflow {lover/users}) want to code your own asynchronous socket. All you need to do is detach native socket handle from connected CFSocketRef object.
我知道我正在提交一个非常古老的问题的答案。如果您(访问者/stackoverflow {lover/users})想要编写自己的异步套接字。您需要做的就是从连接的 CFSocketRef 对象中分离本机套接字句柄。
void TCPClientCallBackHandler(CFSocketRef s, CFSocketCallBackType callbacktype,CFDataRef address, const void *data,void *info){
ClientSocket *obj_client_ptr=(__bridge ClientSocket*)info;
switch (callbacktype) {
case kCFSocketConnectCallBack :
if(data){
[obj_client_ptr StopClient];
}
else{ //detach socket started
CFSocketNativeHandle handle=CFSocketGetNative(s);
CFSocketSetSocketFlags(s, 0);
CFSocketInvalidate(s);
CFRelease(s);
s=nil;//detach socket ended
[obj_client_ptr ConfigureStream:handle];
// handle pass to CFStreamCreatePairWithSocket then bridge to NSStream
}
break;
}
}
if still not got it, then watch it on youtube: https://www.youtube.com/watch?v=bJP4nysTmnI
如果还没有明白,请在 youtube 上观看:https: //www.youtube.com/watch?v=bJP4nysTmnI