我在开发High Sierra应用时,创建socket遇到了严重的问题。如果我创建命令行工具,一切正常!我可以创建socket并绑定它。但是,如果我尝试创建Cocoa应用程序,我就无法绑定我的socket!:(
如果我在Cocoa应用程序中使用CFSockets,
如果我在Cocoa应用程序中使用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应用程序中使用低级别的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中)? 出了什么问题? :(
帮帮我!:(((