直到最近,以下代码在我的项目中均能正常工作。但是从几天前开始,它不再起作用。我可以将NSLog
语句替换为printf
语句,替换其他Obj-C样式的语句,并在终端中使用g ++进行编译。
它应该只连接到Raspberry Pi上一个非常原始的服务器,发送一个字符“ R”,并读回一个2字节的整数。几个月前,当我在XCode中编译或运行它时,它就起作用了。当我现在使用g ++在终端中编译时,它可以工作。但是,当我现在在XCode中运行时,它无法打开套接字并报告setDAC: connection failed
。
我担心我会发疯。苹果是否隐藏了一些我需要在XCode 9.4.1中打开网络访问权限的新设置?有什么建议吗?
以前在XCode中起作用的代码:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include "stdio.h"
.
.
.
float readDAC(uint8_t ch){
if(!isConnected){
const char *servIP = [[txtIPAddress stringValue] UTF8String];
in_port_t servPort = 5001;
int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock < 0){
NSLog(@"setDAC: Socket creation failed\n");
ok = false;
}
struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
int rtnVal = inet_pton(AF_INET, servIP, &servAddr.sin_addr.s_addr);
if(ok){
if(rtnVal == 0){
NSLog(@"setDAC: inet_pton() failed: invalid address string\n");
ok = false;
}
else if (rtnVal < 0){
NSLog(@"setDAC: inet_pton() failed\n");
ok = false;
}
servAddr.sin_port = htons(servPort);
}
if(ok) if(connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0){
NSLog(@"setDAC: connection failed\n");
ok = false;
}
datastream = fdopen(sock, "r+");
isConnected = true;
}
//send 'R' to read
//send 'W' to write
char writeChar = 'R';
if([AD5754 intValue]==1){
uint8_t writeChannel;
int16_t setVal;
float theVal;
uint8_t nDAC = 0;
if(ch>3) nDAC = 1;
ch = ch%4;
ch = 16*nDAC+ch;
writeChannel = ch;
fwrite(&writeChar, sizeof(writeChar), 1, datastream);
fwrite(&writeChannel, sizeof(writeChannel), 1, datastream);
fread(&setVal, sizeof(setVal), 1, datastream);
int16_t theSetVal;
theSetVal = ntohs(setVal);
theVal = (float)theSetVal/100;
NSLog(@"Read channel %i: %0.2f", ch, theVal);
fflush(datastream);
fclose(datastream);
return theVal;
}
答案 0 :(得分:0)
我向Apple支付了99美元的年费,以加入开发人员计划,现在网络编码又可以使用了。对苹果没有印象,但是还可以。
如果记录了该功能或发出了一些通知,我不介意为恢复该功能而付费。但是我挣扎了几天,直到绝望地试图随机地把钱扔给这个问题。