Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a string in a UDP socket in iOS7?

I am trying to send a simple string over UDP in my iOS7 app to a known IP and could not find a simple explanation and sample code on how to do that. There is plenty out there about TCP but not so much about UDP and it has to be UDP in my case.

like image 531
Michal Shatz Avatar asked Jan 18 '14 17:01

Michal Shatz


2 Answers

You could use https://github.com/robbiehanson/CocoaAsyncSocket, which is an Objective-C wrapper for TCP and UDP connections. It also contains sample code for TCP and UPD clients and servers.

like image 117
Martin R Avatar answered Nov 17 '22 07:11

Martin R


You could use this wrapper-less simple gist.

UDPEchoClient.h

#import <Foundation/Foundation.h>

@interface UDPEchoClient : NSObject

- (BOOL) sendData:(const char *)msg;

@end

UDPEchoClient.m

#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

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
{
    //
    //  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;
    }
}
like image 30
Ratul Sharker Avatar answered Nov 17 '22 08:11

Ratul Sharker