Running a sample application in background mode(ios) using TCP sockets.It give message(a local notification ) when client send any message and wait for another message.It works fine when the application run in debugging, but crashes in release mode after 10 seconds.
used: xcode 4.2 & iPad 2 for testing..
This is the code I'm working with:
struct sockaddr_in serv_addr, cli_addr;
CFReadStreamRef hReadStream;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) printf("Sockfd Error\n");
bzero((char *) &serv_addr, sizeof(serv_addr));
portNo = 3600;//atoi(argv);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portNo);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
printf("bind error\n");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = 1;
while(newsockfd)
{
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
NSLog(#"newsockfd create %d\n",newsockfd);
remoteAdd = inet_ntoa(cli_addr.sin_addr);
//cout << remoteAdd << " port = " << cli_addr.sin_port << endl;
portNo = cli_addr.sin_port;
#if defined(__IPHONE_4_0) && !(TARGET_IPHONE_SIMULATOR)
NSLog(#"newsocket %d\n",newsockfd);
// Only do this if it is a SipSocket we are watching
if (newsockfd)
{
// Set it to non-blocking
//int set = 0;
//ioctl(newsockfd, FIONBIO, reinterpret_cast<int>(&set));
CFStreamCreatePairWithSocket (kCFAllocatorDefault, newsockfd,
&hReadStream, NULL);
if (CFReadStreamSetProperty(hReadStream,
kCFStreamNetworkServiceType,
kCFStreamNetworkServiceTypeVoIP) != TRUE)
{
// An error occured, delete the stream
if(hReadStream != NULL)
{
CFReadStreamClose(hReadStream);
CFRelease(hReadStream);
hReadStream = NULL;
}
//return -1;
}
else NSLog(#" >>>>>>>>>>>>>>>>>>> property set Here <<<<<<<<<<<<<<<<\n");
if (CFReadStreamOpen(hReadStream) != TRUE)
{
// An error occured, delete the stream
if(hReadStream != NULL)
{
CFReadStreamClose(hReadStream);
CFRelease(hReadStream);
hReadStream = NULL;
}
// return -1;
}
else NSLog(#"read....\n");
}
#endif
[self test];
}
Crash report in device log :
"Application 'Demo' exited abnormally with signal 9: Killed: 9" &
"unknown ReportCrash[10915] <Error>: Saved crashreport to
/var/mobile/Library/Logs/CrashReporter/servertest_2011-12-13-163920_EyeBalls-iPad.plist
using uid: 0 gid: 0, synthetic_euid: 501 egid: 0"
Trying to found the bug but it works fine in debug mode Exactly no error found in device log or debug window..Please help me :(
you can switch your compiler option to llvm gcc4.2 . I guess it issue related to clang.
hope it will work fine.
Related
I try to use require('electron').clipboard.readText() and just get an empty string, although I have some text in the clipboard.
I see this in Console.app (not sure if this is related):
Failed to set up CFPasteboardRef 'Apple CFPasteboard general'. Error: <error: 0x7fffa6d6fda0> { count = 1, transaction: 0, voucher = 0x0, contents =
"XPCErrorDescription" => <string: 0x7fffa6d70048> { length = 18, contents = "Connection invalid" }
}
How can I fix this?
I think this is because the Electron app was started via execve() in a forked process (fork() +daemon()` actually).
One workaround is to execute /usr/bin/open as a wrapper, like so (pseudo code):
open -a argv[0] --args args[1...]
Or basically this code:
char** args = parse_args(cmd);
char* arg0 = find_in_path(args[0]);
pid_t pid = fork();
if (pid == 0) {
daemon(1, 0);
#ifdef __APPLE__
{
// We cannot directly use `execv` for a GUI app on MacOSX
// in a forked process
// (e.g. issues like https://stackoverflow.com/questions/53958926/).
// But using `open` will work around this.
int argc = 0;
for(; args[argc]; ++argc);
char** args_ext = malloc(sizeof(char*) * (argc + 5));
arg0 = "/usr/bin/open";
args_ext[0] = arg0;
args_ext[1] = "-a";
args_ext[2] = args[0];
args_ext[3] = "--args";
for(int i = 0; ; ++i) {
args_ext[i + 4] = args[i + 1];
if(!args[i + 1])
break;
}
args = args_ext;
}
#endif
execv(arg0, args);
exit(-1);
} else if (pid > 0) { // master
// go on ...
free(args);
} else {
// error handling...
}
(Basically via this commit.)
In C language, I want to develop an application to run bluetoothctl, and then send command to it and receive the info, like we run bluetoothctl in console.
int main(void)
{
int fd, err = 0;
char* tty;
wait_pid = -1;
printf("BLEapp program start.......\r\n");
if (wait_pid == -1) {
printf("Start to run bluetoothctl......\r\n");
if ((pid = fork()) < 0) {
err = 1;
printf("Create new progress failed.\r\n");
}
else if (pid == 0) {
// Child progress, run as default when xid = 0x0 and mastAddr = 0x0
execl("/root/bluez-5.42/client/bluetoothctl", "bluetoothctl", NULL);
}
}
else {
printf("The child procress is still running.....\r\n");
err = 1;
}
//tty = ttyname(STDIN_FILENO);
//printf("stdin name is %s.\r\n", tty);
//fd = open(tty, O_WRONLY);
//if (fd < 0) {
// printf("Open stdin error..............\r\n");
//}
while (1) {
sleep(2);
fputs("devices\n", stdin);
//write(fd, "devices\n", 8);
}
return err;
}
I run bluetoothctl as child process, and want to send "devices\n" command to it, and read the listed devices. But it doesn't work.
Please help me to fix this problem.
Thanks.
I'm trying to send some up sensor data from arduino to my iPhone and i found here (http://www.benripley.com/development/ios/udp-server-on-iphone/) and I run it with some small mods to make it compile on a viewController I display the data and everything works perfectly. But when I leave the view and go back, probably because it tries to start the server again, the app crashes. i tried to move the code to the appDelegate to start the server on launch but the app crashes on launch. even if I try only to start the server on launch and start reading by the time I enter the view and the same thing happens. Any idea?
here is the code:
#include < sys/socket.h >
#include < arpa/inet.h >
#include < errno.h >
#define UDP 17
typedef struct sockaddr_in sockaddr_in;
- (void)startServer {
NSLog(#"UDP Server started...");
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = INADDR_ANY;
sa.sin_port = htons(5009);
// bind the socket to our address
if (-1 == bind(sock,(struct sockaddr *)&sa, sizeof(struct sockaddr)))
{
perror("error bind failed");
close(sock);
exit(EXIT_FAILURE);
}
for (;;)
{
recsize = recvfrom(sock,
(void *)buffer,
1024,
0,
(struct sockaddr *)&sa,
&fromlen);
if (recsize < 0)
fprintf(stderr, "%s\n", strerror(errno));
msg = [NSString stringWithFormat:#"%s", buffer];
NSLog(msg);
}
}
and the actual call is this:
[NSThread detachNewThreadSelector:#selector(startServer)
toTarget:self
withObject:nil];
When I comment out the code that turn off blocking everything works fine, but when I leave the non blocking code in I never get any data from recvfrom. (Received bytes is always 0) Here is the code with the blocking statement commented out. I am new to Xcode and IOS but have created UDP code for .NET.
// Create Socket
int Handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (Handle <=0){
printf("Failed to create socket\n");
}
// Bind Socket
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons((unsigned short) 4966);
if (bind(Handle,(struct sockaddr *) &address, sizeof(struct sockaddr_in))< 0){
printf("Failed to Bind\n");
}//end if
printf("Bind Done\n");
// Set to non Blocking
int NonBlocking = 1;
//if (fcntl(Handle, F_SETFL, O_NONBLOCK, NonBlocking) == -1){
printf("Faile to set nonblocking\n");
//}
Boolean ContinueLoop = true;
unsigned char Packet_Data[256];
unsigned int Maximum_Packet_Size = sizeof(Packet_Data);
struct sockaddr_in From_Address;
socklen_t FromLength = sizeof(From_Address);
while (ContinueLoop){
int Received_Bytes = recvfrom(Handle, (char *)Packet_Data, Maximum_Packet_Size, 0, (struct sockaddr *)&From_Address, &FromLength);
if (Received_Bytes > 0){
ContinueLoop = false;
printf("Got Data \n");
} else {
perror("Now");
printf(".");
}
close(Handle);
}//wend
Found the problem. I accidentally included the close statement inside the while loop. Moved it out of the loop where it belongs and all is working now. Blocking and nonblocking.
I'm working through Beej's sockets tutorial. Why is the call to socket below not working in the iPhone simulator?
int status;
struct addrinfo hints;
struct addrinfo *servinfo;
char ipstr[INET6_ADDRSTRLEN];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((status = getaddrinfo("www.yahoo.com",
"80",
&hints,
&servinfo)) != 0) {
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
for(struct addrinfo *p = servinfo; p != NULL; p = p->ai_next) {
void *addr;
char *ipver;
if (p->ai_family == AF_INET) {
struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr;
addr = &(ipv4->sin_addr);
ipver = "IPv4";
} else {
struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr;
addr = &(ipv6->sin6_addr);
ipver = "IPv6";
}
inet_ntop(p->ai_family, addr, ipstr, sizeof(ipstr));
printf(" %s: %s\n", ipver, ipstr);
int socketfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (socketfd)
printf("errno: %d\n", errno);
}
freeaddrinfo(servinfo);
The output of the above code is:
IPv4: 72.30.38.140
errno: 2
IPv4: 72.30.2.43
errno: 2
errno 2 is No such file or directory. I don't know how to interpret this.
The error is in this line:
if (socketfd) {
This should be:
if (socketfd == -1) {
since socket() returns -1 on error, not zero on success (as mentioned here: https://stackoverflow.com/a/1879234/22471)