我在为 Hight Sierra 的应用程序中创建套接字时遇到了严重而奇怪的问题。如果我创建命令行工具,一切正常!我创建套接字,绑定套接字。但是如果我尝试创建 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;
}
我收到这条消息:
CFSocketSetAddress 绑定失败:1
如果我在 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);
...它也不起作用,我在绑定时遇到错误,并且在控制台中也收到此消息:
错误:不允许操作
但是在命令行工具中一切正常!
我需要修复什么样的项目设置(可能在 info.plist 中)?哪里有麻烦?:(
帮我!:(((