I'm running across the wierdiest problem. I'm converting resolving a Bonjour discovered name to get it's IP address and when this works fine. However, I need to save the IP address so I'm trying to copy it to a global variable. When I'm in the callback, I do the strcpy and then follow it up with a printf to verify the copy is done and all shows fine. However, as soon as I exit the callback, the global is still got the defaul value of "0.0.0.0". I have other callbacks which access globals. Why can't I copy this data into a globa???
Below is a small piece of code that shows the global and the callback C function.
Thanks
Dalton Hamilton
============================
// Global defined here
char service_ipaddress[256]="0.0.0.0";
static void
MyResolveCallback(CFNetServiceRef service, CFStreamError* error, void* info)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
struct sockaddr *socketAddress;
CFArrayRef addresses;
uint16_t port;
int count;
char buffer[256];
addresses = CFNetServiceGetAddressing(service);
/* Search for the IPv4 addresses in the array. */
for (count = 0; count < CFArrayGetCount(addresses); count++) {
socketAddress = (struct sockaddr *)CFDataGetBytePtr(CFArrayGetValueAtIndex(addresses, count));
/* Only continue if this is an IPv4 address. */
if (socketAddress && socketAddress->sa_family == AF_INET) {
if (inet_ntop(AF_INET, &((struct sockaddr_in *)socketAddress)->sin_addr, buffer, sizeof(buffer)))
{
port = ntohs(((struct sockaddr_in *)socketAddress)->sin_port);
strcpy(service_ipaddress,buffer);
printf("[buffer] IP Address = %s\n", buffer);
printf("Port Number = %d\n", port);
printf("[ip] IP Address = %s\n", service_ipaddress);
/* Now that you know the IP address and port number, you
should attempt to connect to the service. If the connection
succeeds, then cancel the Resolve operation. If the connection
fails, keep the Resolve running just in case your Resolve
callback gets called again with another IPv4 address, and then
try connecting to the new address. */
}
}
}
[pool release];
return;
}