5

我试图在我的 iOS7 应用程序中通过 UDP 向已知 IP 发送一个简单的字符串,但找不到有关如何执行此操作的简单解释和示例代码。有很多关于 TCP 的内容,但关于 UDP 的内容不多,在我的情况下它必须是 UDP。

4

2 回答 2

3

您可以使用https://github.com/robbiehanson/CocoaAsyncSocket,它是用于 TCP 和 UDP 连接的 Objective-C 包装器。它还包含 TCP 和 UPD 客户端和服务器的示例代码。

于 2014-01-18T17:44:14.083 回答
2

您可以使用这个无包装的简单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;
    }
}
于 2016-06-23T13:09:47.210 回答