iPhone 设备上的 C Socket 崩溃。 EXC_BAD_ACCESS
我的代码在模拟器上运行良好,但一旦我在设备上尝试它,程序就会崩溃。
它发生在初始化套接字时。这是我的代码以及运行/调试吐出的内容。 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
代替
,或者您可以使用
server_addr.sin_addr.s_addr = inet_addr(IP);
这只是为了避免崩溃。但我不确定为什么
server_addr.sin_addr = *((struct in_addr *)server->h_addr);
会崩溃。编辑:
试试这个:
这不需要
server = gethostbyname(SERVER_NAME);
我注意到的另一件事是:
这样可以吗?我的意思是两个语句中的不同协议?
Try using
in place of
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:
This does not require
server = gethostbyname(SERVER_NAME);
One more thing that I noted is:
Is this alright. I mean different protocols in two statements?