iPhone 设备上的 C Socket 崩溃。 EXC_BAD_ACCESS

发布于 2024-11-09 02:33:30 字数 3491 浏览 0 评论 0原文

我的代码在模拟器上运行良好,但一旦我在设备上尝试它,程序就会崩溃。

它发生在初始化套接字时。这是我的代码以及运行/调试吐出的内容。 (SERVER_NAME 是运行服务器的机器的主机名)

Error:

Program received signal:  “EXC_BAD_ACCESS”.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J3)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).

in .h

    int connectionSocket;
struct hostent *server;
struct sockaddr_in server_addr;

in .m

- (void)viewDidLoad {
[super viewDidLoad];

TRANSFER_COMPLETED = false;
TIME_COMPLETED = false;

//[UIApplication sharedApplication].idleTimerDisabled = YES;    //Disable app sleep (check this out)


//Build connection to Server
portNumber = PORT_NUM;
connectionSocket = socket(PF_INET, SOCK_STREAM, 0); 
server = gethostbyname(SERVER_NAME);
server_addr.sin_family = AF_INET;     
server_addr.sin_port = htons(portNumber);   
//server_addr.sin_addr = *((struct in_addr *)server->h_addr);   
memcpy ( &( server_addr.sin_addr ), server->h_addr, server->h_length );

if (connect(connectionSocket, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) 
{
    perror("Connect");
    exit(1);
}   

//Setup Battery monitoring and Device characteristics
myDevice = [UIDevice currentDevice];
myDevice.batteryMonitoringEnabled = YES;
NSString* sysName = myDevice.systemName;
NSString* sysVersion = myDevice.systemVersion;

//Name the file for this device

NSString* myString = [NSString stringWithFormat:@"systemName:%@ systemVersion:%@ \n", sysName, sysVersion];

const char* deviceInfo = malloc(128);
char* deviceSettings = malloc(256);

deviceInfo = [myString UTF8String];

//Get the battery State
int intBatteryState = myDevice.batteryState;
char* batteryState = malloc(24);
switch(intBatteryState){
    case 0: 
        batteryState = "Unknown";
        break;
    case 1:
        batteryState = "Unplugged";
        break;
    case 2:
        batteryState = "Charging";
        break;
    case 3:
        batteryState = "plugged and full";
        break;
    default:
        batteryState = "Error";
        break;
}   

sprintf(deviceSettings, "iOS Test *** APPLICATION_BUFFER_SIZE(B): %i, SLEEP_TIME(microseconds) per app buffer size: %i, TOTAL_BYTES_EXPECTED per download: %i, TOTAL_DOWNLOADS: %i, TEST_TIME(s): %i, Battery State:%s \n", (int) round(APPLICAION_BUFFER_SIZE), SLEEP_TIME, TOTAL_BYTES_EXPECTED, TOTAL_DOWNLOADS, TEST_TIME, batteryState);

send(connectionSocket, DEVICE_NAME, strlen(DEVICE_NAME), 0);
sleep(1);
send(connectionSocket, deviceSettings, strlen(deviceSettings), 0);
send(connectionSocket, deviceInfo, strlen(deviceInfo), 0);

free(deviceSettings);
//free(deviceInfo);//Says it is being freed without being allocated but I allocated it with malloc...
//free(batteryState); 

//  [NSThread detachNewThreadSelector:@selector(download) toTarget:self withObject:nil];

// [NSThread detachNewThreadSelector:@selector(keepRunning) toTarget:self withObject:nil]; // [NSThread detachNewThreadSelector:@selector(busyLoop) toTarget:self withObject:nil]; //

//  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelDidChange) name:UIDeviceBatteryLevelDidChangeNotification object:nil];

// [[NSNotificationCenter defaultCenter] addObserver:self 选择器:@selector(batteryStateDidChange) name:UIDeviceBatteryStateDidChangeNotification object:nil];

I have code that runs well on the simulator but as soon as I try it on the device the program is crashing.

It happens when initializing the socket.. here is my code and what run/debug spits out. (SERVER_NAME is the hostname of my machine where the server is running)

Error:

Program received signal:  “EXC_BAD_ACCESS”.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J3)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).

in .h

    int connectionSocket;
struct hostent *server;
struct sockaddr_in server_addr;

in .m

- (void)viewDidLoad {
[super viewDidLoad];

TRANSFER_COMPLETED = false;
TIME_COMPLETED = false;

//[UIApplication sharedApplication].idleTimerDisabled = YES;    //Disable app sleep (check this out)


//Build connection to Server
portNumber = PORT_NUM;
connectionSocket = socket(PF_INET, SOCK_STREAM, 0); 
server = gethostbyname(SERVER_NAME);
server_addr.sin_family = AF_INET;     
server_addr.sin_port = htons(portNumber);   
//server_addr.sin_addr = *((struct in_addr *)server->h_addr);   
memcpy ( &( server_addr.sin_addr ), server->h_addr, server->h_length );

if (connect(connectionSocket, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) 
{
    perror("Connect");
    exit(1);
}   

//Setup Battery monitoring and Device characteristics
myDevice = [UIDevice currentDevice];
myDevice.batteryMonitoringEnabled = YES;
NSString* sysName = myDevice.systemName;
NSString* sysVersion = myDevice.systemVersion;

//Name the file for this device

NSString* myString = [NSString stringWithFormat:@"systemName:%@ systemVersion:%@ \n", sysName, sysVersion];

const char* deviceInfo = malloc(128);
char* deviceSettings = malloc(256);

deviceInfo = [myString UTF8String];

//Get the battery State
int intBatteryState = myDevice.batteryState;
char* batteryState = malloc(24);
switch(intBatteryState){
    case 0: 
        batteryState = "Unknown";
        break;
    case 1:
        batteryState = "Unplugged";
        break;
    case 2:
        batteryState = "Charging";
        break;
    case 3:
        batteryState = "plugged and full";
        break;
    default:
        batteryState = "Error";
        break;
}   

sprintf(deviceSettings, "iOS Test *** APPLICATION_BUFFER_SIZE(B): %i, SLEEP_TIME(microseconds) per app buffer size: %i, TOTAL_BYTES_EXPECTED per download: %i, TOTAL_DOWNLOADS: %i, TEST_TIME(s): %i, Battery State:%s \n", (int) round(APPLICAION_BUFFER_SIZE), SLEEP_TIME, TOTAL_BYTES_EXPECTED, TOTAL_DOWNLOADS, TEST_TIME, batteryState);

send(connectionSocket, DEVICE_NAME, strlen(DEVICE_NAME), 0);
sleep(1);
send(connectionSocket, deviceSettings, strlen(deviceSettings), 0);
send(connectionSocket, deviceInfo, strlen(deviceInfo), 0);

free(deviceSettings);
//free(deviceInfo);//Says it is being freed without being allocated but I allocated it with malloc...
//free(batteryState); 

//  [NSThread detachNewThreadSelector:@selector(download) toTarget:self withObject:nil];

// [NSThread detachNewThreadSelector:@selector(keepRunning) toTarget:self withObject:nil];
// [NSThread detachNewThreadSelector:@selector(busyLoop) toTarget:self withObject:nil];
//

//  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryLevelDidChange) name:UIDeviceBatteryLevelDidChangeNotification object:nil];

// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(batteryStateDidChange) name:UIDeviceBatteryStateDidChangeNotification object:nil];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

前事休说 2024-11-16 02:33:30

尝试使用

memcpy ( &( serv_addr.sin_addr ), server->h_addr, server->h_length );

代替

server_addr.sin_addr = *((struct in_addr *)server->h_addr);  

,或者您可以使用server_addr.sin_addr.s_addr = inet_addr(IP);

这只是为了避免崩溃。但我不确定为什么 server_addr.sin_addr = *((struct in_addr *)server->h_addr); 会崩溃。

编辑:
试试这个:

portNumber = PORT_NUM;
connectionSocket = socket(AF_INET, SOCK_STREAM, 0); 
server_addr.sin_family = AF_INET;     
server_addr.sin_port = htons(portNumber);   
server_addr.sin_addr.saddr = htonl(INADDR_ANY);

这不需要 server = gethostbyname(SERVER_NAME);

我注意到的另一件事是:

connectionSocket = socket(**PF_INET**, SOCK_STREAM, 0); 
//server = gethostbyname(SERVER_NAME);
server_addr.sin_family = **AF_INET**;  

这样可以吗?我的意思是两个语句中的不同协议?

Try using

memcpy ( &( serv_addr.sin_addr ), server->h_addr, server->h_length );

in place of

server_addr.sin_addr = *((struct in_addr *)server->h_addr);  

OR you can use server_addr.sin_addr.s_addr = inet_addr(IP);

This is just to avoid crash. But I'm not sure why server_addr.sin_addr = *((struct in_addr *)server->h_addr); will crash.

EDIT:
Try this:

portNumber = PORT_NUM;
connectionSocket = socket(AF_INET, SOCK_STREAM, 0); 
server_addr.sin_family = AF_INET;     
server_addr.sin_port = htons(portNumber);   
server_addr.sin_addr.saddr = htonl(INADDR_ANY);

This does not require server = gethostbyname(SERVER_NAME);

One more thing that I noted is:

connectionSocket = socket(**PF_INET**, SOCK_STREAM, 0); 
//server = gethostbyname(SERVER_NAME);
server_addr.sin_family = **AF_INET**;  

Is this alright. I mean different protocols in two statements?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文