iOS app crash while send msg by socket - ios

I use this method for send msg to server
ssize_t send(int, const void *, size_t, int) __DARWIN_ALIAS_C(send);
This is standard Lib from Apple (socket.h and socket.m )
NSString *msg = #"Message want to send";
// socketFileDescriptor is int
NSData *wdata = [msg dataUsingEncoding:NSUTF8StringEncoding];
ssize_t n;
const UInt8 *buf = (const UInt8 *)[wdata bytes];
n = send(socketFileDescriptor, buf,[wdata length],0); // I really sure,it's crash at this statement on device iPad Air, iPad Air2 only
BUT on debug mode isn't not crash!
App will crash when I archive project to IPA (Ad-Hoc Mode)
I have no ideal for this case please help me.
This is more information
I use this method for connect to server
I have already assign value for self.host and self.port variable.
(I sure it can be connect normally in debug mode)
http://pastebin.com/raw.php?i=KFKJsJHZ
and I use this method for send message to server
http://pastebin.com/raw.php?i=Z7Js2JT8
2 method in same class BSDSocket
Please tell me If you want more information.
Thank you

Related

Can I use Unix socket for local communication between two iOS applications

Background: I have two iOS applications that need to exchange data locally with each other. The ideal scenario is that app A calls app B into foreground using URL scheme, with app A in the background, the two apps could exchange data with each other multiple rounds. I can declare app A the ability to run in background(I have valid reason to do so) so background execution for A is not a problem. I don't want to continue using URL scheme for the data exchange part between two apps because using URL scheme will open the other app and that means these two apps will be opened multiple times if there is multiple rounds of data exchange.
Also please note that these two iOS applications do not belong to the same developer so anything that relies on app group to share data will not work in this case...
Currently I have found a solution using a Unix Internet Domain Socket that binds onto the local loopback interface. It is technically working but I don't know if Apple permits this or not? Is there any better way to do this?
This is the code for the unix socket:
For the server app(app A):
BOOL _listenForConnections = false;
int listenfd = 0;
struct sockaddr_in serv_addr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(8008);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
_listenForConnections = true;
listen(listenfd, 10);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(#"Waiting for connections...");
while (_listenForConnections)
{
__block int connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
NSLog(#"Connection accepted");
char buffer[4096];
bzero(buffer, 4096);
NSString *message = #"";
bool continueReading = true;
do{
recv(connfd , buffer , 4096 , 0);
continueReading = false;
NSLog(#"%#", [NSString stringWithFormat: #"%#", [NSString stringWithFormat: #"Received message from client: %#%s", message, buffer]]);
}while (continueReading);
char* answer = "Hello World";
write(connfd, answer, strlen(answer));
NSLog(#"%#", [NSString stringWithFormat: #"Sent response to client"]);
}
NSLog(#"Now stop listening for connections");
close(listenfd);
});

Mxchip IOT dev kit receiving data from IOT hub

I just started playing with the mxchip. I want my device to trigger an alarm when receiving some command. It seems to me all examples I found are device collecting info and send to iot hub. Can mxchip receive data from iothub or azure function? And maybe some guidance on getting started?
Yes, assuming you use the SDK from the examples for the MXChip you have those handlers available:
DevKitMQTTClient_SetMessageCallback(MessageCallback);
DevKitMQTTClient_SetDeviceMethodCallback(DeviceMethodCallback);
Examples:
static void MessageCallback(const char *payLoad, int size)
{
Screen.print(1, payLoad, true);
}
static int DeviceMethodCallback(const char *methodName, const unsigned char *payload,
int size, unsigned char **response, int *response_size)
{
LogInfo("Try to invoke method %s", methodName);
// Do your logic...
int result = 200;
return result;
}
Yes. Its possible to send messages to device ( Cloud-to-device) from IOT Hub.
Here is some example mentioned in these links
https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-c2d
https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-messaging

Get currently foreground running app (third party) bundle Identifier

How to get application name running in foreground? it was working in iOS 7 using SpringBoard but in iOS 8 I don't get the result. Any help is appreciated. I don't need to submit my app on apple store so if any patch or script also available then please let me know.
Code is working fine on below iOS 8.0
- (NSMutableString*) getActiveApps
{
mach_port_t *port;
void *lib = dlopen(SBSERVPATH, RTLD_LAZY);
int (*SBSSpringBoardServerPort)() =
dlsym(lib, "SBSSpringBoardServerPort");
port = (mach_port_t *)SBSSpringBoardServerPort();
dlclose(lib);
//mach_port_t * port = [self getSpringBoardPort];
// open springboard lib
//void *lib = dlopen(SBSERVPATH, RTLD_LAZY);
// retrieve function SBFrontmostApplicationDisplayIdentifier
void *(*SBFrontmostApplicationDisplayIdentifier)(mach_port_t *port, char *result) =
dlsym(lib, "SBFrontmostApplicationDisplayIdentifier");
// reserve memory for name
char appId[256];
memset(appId, 0, sizeof(appId));
// retrieve front app name
SBFrontmostApplicationDisplayIdentifier(port, appId);
// close dynlib
dlclose(lib);
return [[NSString stringWithFormat:#"%s", appId] retain];
}
Also I show new framework FrontBoard anyone know should it will help to out of this problem?
There's no way to do that anymore in iOS 8 since a vulnerability was discovered. A background app could attack though the frontmost app. More details here: http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2014-4361

How to Get Host Name connected to a Socket using CFSocketNativeHandle iOS?

I am using Socket programming to connect devices each other in iOS.
I want to get connected device's Host Name which is connected to a socket using CFSocketNativeHandle.
When other device connected to my Socket i get callback in following callback function:
static void serverAcceptCallback(CFSocketRef socket, CFSocketCallBackType type, CFDataRef address, const void *data, void *info)
{
// We can only process "connection accepted" calls here
if ( type != kCFSocketAcceptCallBack )
{
return;
}
// for an AcceptCallBack, the data parameter is a pointer to a CFSocketNativeHandle
CFSocketNativeHandle nativeSocketHandle = *(CFSocketNativeHandle*)data;
uint8_t name[SOCK_MAXADDRLEN];
socklen_t namelen = sizeof(name);
NSData *peer = nil;
if (0 == getpeername(nativeSocketHandle, (struct sockaddr *)name, &namelen)) {
peer = [NSData dataWithBytes:name length:namelen];
NSString *hostName = [[NSString alloc] initWithData:peer encoding:NSUTF8StringEncoding];
NSLog(#"HostName=%#",hostName);
}
}
Here I am getting NSData for "peer" but i am getting NSString *hostName=null.
How can i get exact Host name with this NSData.
getpeername() returns the address of the peer connected to the socket. You can access it using code below:
struct sockaddr_in addr ;
socklen_t len = sizeof(addr) ;
int res = getpeername(nativeSocketHandle, (struct sockaddr *)&addr, &len) ;
if (res == 0) {
char strAddr[20] ;
inet_ntop(AF_INET, &addr.sin_addr, strAddr, sizeof(strAddr)) ;
printf("ip %s\n", strAddr) ;
}
The only thing the socket knows is the IP address of the other end of the connection, so when you use getpeername it returns you the IP address. You will need to look up the IP address using either CFhost or POSIX calls - there are a squillion references to how to accomplish this using the POSIX APIs.
There is a very good chance that the other end will not be the same as the name of the device.

RTMP Streaming from ios to server?

Right now, I am using a static librtmp.a library to open a RTMP connection between my iphone and server. When the record button is pressed, the camera starts taking input, and on captureoutput, uses AVAssetWriter's in different threads to encode the videos to h264/AAC. The videos are then saved to a specific URL. I'm trying to take these processed frames and send them over my RTMP client using librtmp.
-(void)writeURL:(NSURL*)segmentURL {
NSLog(#"Utilities are writing...");
NSData *segmentData = [NSData dataWithContentsOfURL:segmentURL];
const char* body = [segmentData bytes];
NSLog(#"%i", [segmentData length]);
NSLog(#"%s", body);
RTMPPacket packet = _rtmp->m_write;
RTMPPacket_Alloc(&packet, [segmentData length]);
packet.m_headerType = RTMP_PACKET_SIZE_MEDIUM;
packet.m_packetType = RTMP_PACKET_TYPE_VIDEO;
packet.m_body = (char*)body;
RTMPPacket_Dump(&packet);
RTMP_SendPacket(_rtmp, &packet, TRUE);
//RTMP_Write(_rtmp, packet.m_body, packet.m_nBodySize);
}
crashes everytime on the RTMPPacket_alloc call and I'm unsure what to do. Is this the right way to approach sending the data over the network?
EDIT:
sample output
2013-03-31 22:53:16.163 videoAppPrototype[2567:907] Switching encoders
2013-03-31 22:53:16.179 videoAppPrototype[2567:1703] Encoder switch finished
2013-03-31 22:53:16.220 videoAppPrototype[2567:1703] Upload public.mpeg-4
2013-03-31 22:53:16.223 videoAppPrototype[2567:1703] Utilities are writing...
2013-03-31 22:53:16.230 videoAppPrototype[2567:1703] 339171
DEBUG: RTMP PACKET: packet type: 0x09. channel: 0x00. info 1: 0 info 2: 0. Body size: 0. body: 0x00
EDIT 2:
I changed my code to use RTMP_Write() instead of RTMP_SendPacket().
New Method:
-(void)writeURL:(NSURL*)segmentURL {
NSLog(#"Utilities are writing...");
NSData *segmentData = [NSData dataWithContentsOfURL:segmentURL];
NSUInteger len = [segmentData length] / sizeof(unsigned char);
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [segmentData bytes], len);
free(byteData);
NSLog(#"%i", [segmentData length]);
NSLog(#"First write attempt...");
RTMP_Write(_rtmp, (char *)byteData, len);
NSLog(#"Successful?");
}
Which now crashes at RTMP_Write, as shown in stack trace:
If someone has any idea or needs anymore information, lemme know!

Resources