Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5934743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:09:23+00:00 2026-05-22T15:09:23+00:00

I have code that runs well on the simulator but as soon as I

  • 0

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];

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:09:24+00:00Added an answer on May 22, 2026 at 3:09 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that runs fairly well, but I would like to make
I have this code that runs but never stops. class A { public static
I have some code that compiles and runs on MSVC++ but will not compile
I have a test of C++ code that in most runs passes, but in
Well my problem is the following. I have a piece of code that runs
I have a code that compile without problems. It runs well on the iPhone
I have a JavaScript snippet that runs very well on Firefox and Safari, but
I have some code that runs a model in a loop. Each iteration of
below i have a code that runs in most of my simple programs ..
I have some jQuery code that runs fine until I add the jQuery UI

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.