How to set objective c label with cons char function in c - objective-c

I want to run my function TestConnection(const char) and set the value from it for my label in objective c. Any help would be greatly apprecitated I keep getting this error "Incompatible integer to pointer conversion sending 'char' to parameter of type 'const char *_Nonull' in xcode.
Here is my c code
fucn.c
const char TestConnection(const char *ip)
{
//char ipAddr[] = "133.131.232.131";
char *val = P(ip);
char *valb = "0packets";
if(strcmp(val, valb) == 0)
{
printf("%s\n", "noconnection");
//return "noconneciton";
}
else
{
printf("%s\n", "connected");
//return "connected";
}
return *val;
}
func.h
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
const char TestConnection(const char *ip);
objective c code Viewcont.m
- (void)checkconneciton:(NSString*)ip {
const char *ipc = [ip UTF8String];
//TestConnection(ipc);
//below is the line with the error.
NSString* xx = [NSString stringWithUTF8String:TestConnection(ipc)];
_ip_label.stringValue = xx;
}

TestConnection is defined as returning a single character, not a string.
It needs to return a string -- const char * -- which can then be used to initialize the NSString.

Related

UDP socket server/client on different machine won't communicate

these code are from https://www.geeksforgeeks.org/udp-server-client-implementation-c/. it runs well when run server and client on same machine.
but if run on two different machine in same network( they can ping each other). then server won't receive Hello from client.
I noticed that both server and client set address INADDR_ANY. this means any machine in the local network, Am I right?
and I tried on client side specify server's ip address. server still won't get message from client.
please give me some clue.
thank you.
server.c
// Server side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
int len, n;
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, ( struct sockaddr *) &cliaddr,
&len);
buffer[n] = '\0';
printf("Client : %s\n", buffer);
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &cliaddr,
len);
printf("Hello message sent.\n");
return 0;
}
client.c
// Client side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 8080
#define MAXLINE 1024
// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from client";
struct sockaddr_in servaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);
servaddr.sin_addr.s_addr = INADDR_ANY;
int n, len;
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &servaddr,
sizeof(servaddr));
printf("Hello message sent.\n");
n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, (struct sockaddr *) &servaddr,
&len);
buffer[n] = '\0';
printf("Server : %s\n", buffer);
close(sockfd);
return 0;
}
in client.c
make following change. ( 192.168.1.1 ) is server's IP address. then they talk.
//servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_addr.s_addr = inet_addr("192.168.1.1");

App keeps crashing with malloc issue objective c and c

Question- I want to be able to run my c code over and over again however after the first execution of it in my objective c it crashes and I receive an error in xcode "Thread 1 EXC_BAD_ACESSS (code=1, address=0x0)". It's really strange because the first time I run it works then any time after that it dies on me. Any help would be greatly appreciated.
Here is my c code
funcc.c
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define BUFLEN 1024
#define MYFREE(x) free(x); x = NULL;
char const *ipcc;
char *statuss;
char *both_words;
char *s1;
char *s2;
char *results;
void concat()
{
**// THIS IS THE LINE I RECIEVE THE EXC_BAD_ACCESS ERROR/////************** results
results = malloc(strlen(s1)+strlen(s2)+1);
strcpy(results, s1);
strcat(results, s2);
both_words = results;
return;
}
char* P(const char *ipAddr)
{
int pipe_arr[2];
char buf[BUFLEN];
pipe(pipe_arr);
if(fork() == 0)
{
dup2(pipe_arr[1], STDOUT_FILENO);
execl("/sbin/ping", "ping", "-c 1", ipAddr, (char*)NULL);
}
else //parent
{
wait(NULL);
read(pipe_arr[0], buf, BUFLEN);
char *xxx = deblank(buf);
char *input = xxx;
char delimiter[] = " ";
char *i, *j, *remainder, *context;
int inputLength = strlen(input);
char *inputCopy = (char*) calloc(inputLength + 1, sizeof(char));
strncpy(inputCopy, input, inputLength);
i = strtok_r (NULL, delimiter, &context);
j = strtok_r (NULL, delimiter, &context);
remainder = context;
s1 = i;
s2 = j;
concat();
char *sx = search(both_words);
//getchar();
}
close(pipe_arr[0]);
close(pipe_arr[1]);
return both_words;
}
void ConnectionMain(){
char *val = P(ipcc);
printf("%s\n", val);
char *valb = "0packets";
if(strcmp(val, valb) == 0)
{
printf("%s\n", "noconnection");
statuss = "noconnection";
//free(concat);
//return "noconneciton";
}
else
{
printf("%s\n", "connected");
statuss = "connected";
//free(concat);
//return "connected";
}
s1 = NULL;
s2 = NULL;
results = NULL;
both_words = NULL;
ipcc = NULL;
}
funcc.h
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
extern const char *ipcc;
extern char *statuss;
//const char *TestConnection(const char *ip);
void ConnectionMain();
Here is my objective c code
ViewCtrl.m
- (void)checkconnection
{
ConnectionMain();
//const char *ipc = [ip UTF8String];
//TestConnection(ipc);
char *statuss1 = "noconnection";
NSString* xx = [NSString stringWithUTF8String:statuss];
//printf("%s\n", statuss);
if(strcmp(statuss, statuss1) == 0)
{
_ip_label.stringValue = #"No Connection";
//I TRIED SETTING THESE BACK TO NULL BUT STILL NO LUCK
// I ALSO TRIED FREE(CONCAT) STILL NO LUCK
ipcc = NULL;
statuss = NULL;
statuss1 = NULL;
}
else
{
//I TRIED SETTING THESE BACK TO NULL BUT STILL NO LUCK
// I ALSO TRIED FREE(CONCAT) STILL NO LUCK
_ip_label.stringValue = #"Connection Success";
ipcc = NULL;
statuss = NULL;
statuss1 = NULL;
}
}
- (IBAction)ipButtonClicked:(id)sender {
//THIS IS WHERE I EXECUTE MY C CODE///////*********
NSString *ip_text = _ip_text_box.stringValue;
const char *ipxx = [ip_text cStringUsingEncoding:NSUTF8StringEncoding];
ipcc = ipxx;
[self checkconnection];
}

Objective-C: printing enum values

I was trying to print out the enum constant in Objective-C on Xcode.
The code:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
enum boolean{
no, yes
};
NSLog(#"%d", yes);
}
return 0;
}
I ran this code and all the console is showing me is "(lldb)".
Is it the syntax that I got wrong?
Or am I missing something here?
Also, I tried it different way using typedef:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
#autoreleasepool {
typedef enum {
no, yes
} boolean;
boolean boolVal = yes;
NSLog(#"%d", boolVal);
}
return 0;
}
I suspect I did something wrong with printing out the value, with NSLog().
But I have tried using %i, %#, %d. But the output was same, (lldb).
Are there any different ways to print out the enum values?
You have to give the members of the enum values is you want to print them. Try the following.
enum boolean {
no = 0,
yes = 1
};
NSLog(#"yes = %d",yes);
The previous code outputs the following.
yes = 1

Comparing char with enum

I have an enum defined this way:
typedef enum : unsigned char {
START_DELIMITER = 0xAA,
END_DELIMITER = 0xBB,
} Delimiter;
When I compare the delimiter value with with char byte from const char*, like so:
// data is NSData;
const char *bytes = [data bytes];
if (bytes[0] == START_DELIMITER) { }
The above test is false even though bytes[0] contains 0xAA.
If I define START_DELIMITER as const char, the comparison is true. Why does the test against the enum fails even though the enum is already defined as unsigned char?
The char is signed, and the enum is unsigned. Perhaps the compiler sign-extends before making the comparison?

Get a list of all available network interfaces (en0, en1, en2, etc) with Cocoa?

In my Cocoa application I want to show the user a list of available network interfaces, like Wireshark does:
What is the best way of getting such a list? Does Apple provide a framework for this, or must I use a C API from the standard library or another library?
Better than wrapping ifconfig you shall check the reference of SCNetworkConfiguration which is part of Core Foundation.
Check SCNetworkInterfaceXxx functions for details.
related answer:
Using Cocoa / Objective-C, get currently connected network's security type in Mac OS X
The following code will get all the interfaces from OS X through System Configuration, then use standard C functions to get the IP addresses (where available).
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>
#define IFT_ETHER 0x6
#include <SystemConfiguration/SCDynamicStore.h>
+(void)getInterfaces
{
SCDynamicStoreRef storeRef = SCDynamicStoreCreate(NULL, (CFStringRef)#"FindCurrentInterfaceIpMac", NULL, NULL);
CFPropertyListRef global = SCDynamicStoreCopyValue (storeRef,CFSTR("State:/Network/Interface"));
id primaryInterface = [(__bridge NSDictionary *)global valueForKey:#"Interfaces"];
for (NSString* item in primaryInterface)
{
if(get_iface_address([item UTF8String]))
{
NSString *ip = [NSString stringWithUTF8String:get_iface_address([item UTF8String])];
NSLog(#"interface: %# - %#",item,ip);
} else
NSLog(#"interface: %#",item);
}
}
static char * get_iface_address (char *interface)
{
int sock;
uint32_t ip;
struct ifreq ifr;
char *val;
if (!interface)
return NULL;
/* determine UDN according to MAC address */
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror ("socket");
return NULL;
}
strcpy (ifr.ifr_name, interface);
ifr.ifr_addr.sa_family = AF_INET;
if (ioctl (sock, SIOCGIFADDR, &ifr) < 0)
{
perror ("ioctl");
close (sock);
return NULL;
}
val = (char *) malloc (16 * sizeof (char));
ip = ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr;
ip = ntohl (ip);
sprintf (val, "%d.%d.%d.%d",
(ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, ip & 0xFF);
close (sock);
return val;
}
#include <SystemConfiguration/SCDynamicStore.h>
SCDynamicStoreRef storeRef = SCDynamicStoreCreate(NULL, (CFStringRef)#"FindCurrentInterfaceIpMac", NULL, NULL);
CFPropertyListRef global = SCDynamicStoreCopyValue (storeRef,CFSTR("State:/Network/Interface"));
id primaryInterface = [(__bridge NSDictionary *)global valueForKey:#"Interfaces"];
for (NSString* item in primaryInterface)
{
NSLog(#"%#", item);
}
Your quickest and best bet is to write a wrapper for ifconfig command.