[英]Creating and binding socket on Mac OS Hight Sierra
I have serious and strange problem with creating socket in my application for Hight Sierra.我在为 Hight Sierra 的应用程序中创建套接字时遇到了严重而奇怪的问题。 If I create command Line tool, everything is ok!
如果我创建命令行工具,一切正常! I create socket, bind socket.
我创建套接字,绑定套接字。 But If I trying to create Cocoa App, I can't binding my socket!
但是如果我尝试创建 Cocoa App,我无法绑定我的套接字! :(
:(
If I use CFSockets in Cocoa App,如果我在 Cocoa App 中使用 CFSockets,
char punchline[] = "MESSAGE from Server!";
int yes = 1;
CFSocketContext CTX = {0, punchline, NULL, NULL, NULL};
CFSocketRef TCPServer = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_STREAM, IPPROTO_TCP, kCFSocketAcceptCallBack, (CFSocketCallBack) &AcceptCallBack, &CTX);
if (TCPServer == NULL) return;
setsockopt(CFSocketGetNative(TCPServer), SOL_SOCKET, SO_REUSEADDR, (void *) &yes, sizeof(yes));
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_port = htons(33000);
addr.sin_addr.s_addr = htonl(INADDR_ANY);
NSData *address = [NSData dataWithBytes:&addr length:sizeof(addr)];
if (CFSocketSetAddress(TCPServer, (CFDataRef) address) != kCFSocketSuccess) {
CFRelease(TCPServer);
return;
}
I get this message:我收到这条消息:
CFSocketSetAddress bind failure: 1 CFSocketSetAddress 绑定失败:1
If I use low level C function in Cocoa App, like this:如果我在 Cocoa App 中使用低级 C 函数,如下所示:
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
...it's not work too, and I get error on binding and also this message in console: ...它也不起作用,我在绑定时出错,并且在控制台中出现此消息:
ERROR: Operation not permitted错误:不允许操作
But in command line tool everything is working well!但是在命令行工具中一切正常!
What kind of project settings (may be in info.plist) I need to fix?我需要修复什么样的项目设置(可能在 info.plist 中)? Where is a trouble?
哪里出问题了? :(
:(
Help me!帮我! :(((
:(((
The problem was that the application was sandboxed and did not have the Network: Incoming Connections entitlement.问题是该应用程序被沙盒化并且没有网络:传入连接权利。 That entitlement can be added in Xcode under the App Sandbox details in the Capabilities tab of the target settings.
可以在 Xcode 中目标设置的 Capabilities 选项卡中的 App Sandbox 详细信息下添加该权利。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.