Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 不能再在XCode中进行网络编程了_Sockets_Cocoa_Tcp_Xcode9.4 - Fatal编程技术网

Sockets 不能再在XCode中进行网络编程了

Sockets 不能再在XCode中进行网络编程了,sockets,cocoa,tcp,xcode9.4,Sockets,Cocoa,Tcp,Xcode9.4,直到最近,以下代码在我的项目中仍然运行良好。但从几天前开始,它就不起作用了。我可以用printf语句替换NSLog语句,替换其他Obj-C风格的语句,并在终端中用g++编译,它工作得很好 它应该只连接到Raspberry Pi上的一个非常原始的服务器,发送一个字符“R”,然后读回一个2字节的整数。几个月前,当我在XCode中编译或运行它时,它工作了。当我现在用g++在终端中编译时,它可以工作。但是,当我现在在XCode中运行时,它无法打开套接字并报告setDAC:connection faile

直到最近,以下代码在我的项目中仍然运行良好。但从几天前开始,它就不起作用了。我可以用
printf
语句替换
NSLog
语句,替换其他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;
#包括
#包括
#包括
#包括
#包括
#包括
#包括“stdio.h”
.
.
.
浮点读数据转换器(uint8\u t ch){
如果(!未连接){
const char*servIP=[[txtIPAddress stringValue]UTF8String];
in_port_t servPort=5001;
int sock=socket(AF_INET、sock流、IPPROTO_TCP);
if(sock<0){
NSLog(@“setDAC:套接字创建失败\n”);
ok=假;
}
servAddr中的结构sockaddr_;
memset(&servAddr,0,sizeof(servAddr));
servAddr.sin_family=AF_INET;
int rtnVal=inet\u pton(AF\u inet、servIP和servAddr.sin\u addr.s\u addr);
如果(确定){
如果(rtnVal==0){
NSLog(@“setDAC:inet_pton()失败:无效的地址字符串\n”);
ok=假;
}
否则如果(rtnVal<0){
NSLog(@“setDAC:inet_pton()失败\n”);
ok=假;
}
servAddr.sin_port=htons(servPort);
}
if(确定)if(连接(sock,(struct sockaddr*)和servAddr,sizeof(servAddr))<0{
NSLog(@“setDAC:连接失败\n”);
ok=假;
}
datastream=fdopen(sock,“r+”);
断开连接=正确;
}
//发送“R”以读取
//发送“W”进行写入
char writeChar='R';
如果([AD5754 intValue]==1){
uint8_t writeChannel;
int16_t setVal;
浮出水面;
uint8_t nDAC=0;
如果(ch>3)nDAC=1;
ch=ch%4;
ch=16*nDAC+ch;
writeChannel=ch;
fwrite&writeChar,sizeof(writeChar),1,datastream;
fwrite&writeChannel,sizeof(writeChannel),1,数据流;
fread(&setVal,sizeof(setVal),1,datastream);
国际电视台;
theSetVal=ntohs(setVal);
val=(浮动)theSetVal/100;
NSLog(@“读取通道%i:%0.2f”,ch,theVal);
fflush(数据流);
fclose(数据流);
返回值;

}

我向苹果支付了99美元的年费来加入开发者计划,现在网络编码又开始工作了。我对苹果没什么印象,但还行


如果有文件记录或发出通知,我不介意花钱恢复功能。但我挣扎了几天,然后绝望地尝试在这个问题上随机投入资金。

我还尝试使用更现代、完全Obj-C的方法,得到了相同的一般行为。