Skip to content

Instantly share code, notes, and snippets.

@ratulSharker
Last active January 9, 2022 10:33
Show Gist options
  • Save ratulSharker/3b6bce0debe77fd96344e14566b23e06 to your computer and use it in GitHub Desktop.
Save ratulSharker/3b6bce0debe77fd96344e14566b23e06 to your computer and use it in GitHub Desktop.

Revisions

  1. ratulSharker revised this gist Jun 22, 2016. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions UDPEchoClient.m
    Original 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
    {
    //
  2. ratulSharker created this gist Jun 22, 2016.
    7 changes: 7 additions & 0 deletions UDPEchoClient.h
    Original 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
    101 changes: 101 additions & 0 deletions UDPEchoClient.m
    Original 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;
    }
    }