Last active
January 9, 2022 10:33
-
-
Save ratulSharker/3b6bce0debe77fd96344e14566b23e06 to your computer and use it in GitHub Desktop.
Revisions
-
ratulSharker revised this gist
Jun 22, 2016 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,6 +11,14 @@ #define IP "host ip" #define PORT host_port static void dataAvailableCallback(CFSocketRef s, CFSocketCallBackType type, CFDataRef address, const void *data, void *info) { // // receiving information sent back from the echo server // CFDataRef dataRef = (CFDataRef)data; NSLog(@"data recieved (%s) ", CFDataGetBytePtr(dataRef)); } @implementation UDPEchoClient { // -
ratulSharker created this gist
Jun 22, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ #import <Foundation/Foundation.h> @interface UDPEchoClient : NSObject - (BOOL) sendData:(const char *)msg; @end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,101 @@ #import "UDPEchoClient.h" // // CFSocket imports // #import <CoreFoundation/CoreFoundation.h> #import <sys/socket.h> #import <arpa/inet.h> #import <netinet/in.h> #define IP "host ip" #define PORT host_port @implementation UDPEchoClient { // // socket for communication // CFSocketRef cfsocketout; // // address object to store the host details // struct sockaddr_in addr; } - (instancetype)init { self = [super init]; if (self) { // // instantiating the CFSocketRef // cfsocketout = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketDataCallBack, dataAvailableCallback, NULL); memset(&addr, 0, sizeof(addr)); addr.sin_len = sizeof(addr); addr.sin_family = AF_INET; addr.sin_port = htons(PORT); addr.sin_addr.s_addr = inet_addr(IP); // // set runloop for data reciever // CFRunLoopSourceRef rls = CFSocketCreateRunLoopSource(kCFAllocatorDefault, cfsocketout, 0); CFRunLoopAddSource(CFRunLoopGetCurrent(), rls, kCFRunLoopCommonModes); CFRelease(rls); } return self; } // // returns true upon successfull sending // - (BOOL) sendData:(const char *)msg { // // checking, is my socket is valid // if(cfsocketout) { // // making the data from the address // CFDataRef addr_data = CFDataCreate(NULL, (const UInt8*)&addr, sizeof(addr)); // // making the data from the message // CFDataRef msg_data = CFDataCreate(NULL, (const UInt8*)msg, strlen(msg)); // // actually sending the data & catch the status // CFSocketError socketErr = CFSocketSendData(cfsocketout, addr_data, msg_data, 0); // // return true/false upon return value of the send function // return (socketErr == kCFSocketSuccess); } else { NSLog(@"socket reference is null"); return false; } }