Local and remote sockets on iphone

I want to send OSC messages from iphone to another program (max / msp) by creating and connecting to the udp socket. this works with the iphone simulator, that is, when both applications work on the same computer, but not when I install the application on the phone itself.

I think the problem might be specifying the IP of the remote computer. I use the sockaddr_in structure to specify IP and port information. when I run the code in the simulator, it is normal to specify IP as INADDR_ANY:

sin_addr.s_addr = INADDR_ANY;

when I run it on the device, I try to convert my IP to a hexadecimal number and indicating that instead of INADDR_ANY. This does not work for the simulator or for the device.

The console shows that the socket is connecting and transmitting data in order, but the remote program (max / msp) does not receive any data at all.

I tried to import the correct frameworks so that it works both on the device and on the simulator.

full code follows:

import "UDPSocketCreate.h"

@implementation UDPSocketCreate

-(id)init
{
    in_addr_t myAddress = 0xC0A80145;
    if(self =[super init])
    {

// addr is an instance variable of type struct sockaddr_in

        memset(&addr, 0, sizeof(addr));
        addr.sin_len = sizeof(addr); 
        addr.sin_family = PF_INET;
        addr.sin_port = htons(3333);
        addr.sin_addr.s_addr = myAddress;INADDR_ANY
        connectAddr = CFDataCreate(NULL, (unsigned char *)&addr, sizeof(addr));

        OSC_initBuffer(&myOSCBuff, sizeof(packetBuffer), packetBuffer);

        NSString *address = @"/test";
        const char *utf8Address = [address UTF8String];
        int addressResult = OSC_writeAddress(&myOSCBuff, (char*)utf8Address);       
    }

    return self;

}


CFSocketRef udpSocket;

// this method is called from the application delegate after init

-(void)createUDPSocketRef
{


    udpSocket = CFSocketCreate(NULL, PF_INET, SOCK_DGRAM, IPPROTO_UDP, kCFSocketWriteCallBack, myCallBack, NULL);
    if(udpSocket == NULL)
    {       
        NSLog(@"socket create failed");  
        return;
    }

    CFRunLoopSourceRef runLoopSrceRef = CFSocketCreateRunLoopSource(NULL, udpSocket, 1);

    CFRunLoopRef rl = CFRunLoopGetCurrent();

    CFRunLoopAddSource(rl, runLoopSrceRef, kCFRunLoopCommonModes);

}

// pressing a button on the UI triggers this method
-(void)bang
{   
    int myInt = 1;  

    int writeRestult = OSC_writeIntArg(&myOSCBuff, myInt);  

    int buffDoneResult;
    if (buffDoneResult = OSC_isBufferDone(&myOSCBuff))
    {
        NSLog(@"valid message in buff");        
        char *pack = OSC_getPacket(&myOSCBuff);
        int packSize = OSC_packetSize(&myOSCBuff);          

        CFDataRef OSCPacketWithAddressTest = CFDataCreate(NULL, pack, packSize);


        CFSocketError sendError = CFSocketSendData(udpSocket, connectAddr, OSCPacketWithAddressTest, 30);
        NSLog(@"send error: %d", sendError);
    }

    OSC_resetBuffer(&myOSCBuff);
    NSString *address = @"/test";
    const char *utf8Address = [address UTF8String];
    int addressResult = OSC_writeAddress(&myOSCBuff, (char*)utf8Address);   

}

@end

any help would be greatly appreciated

+3
source share
2 answers

Edit

in_addr_t myAddress = 0xC0A80145

to

in_addr_t myAddress = inet_addr("192.168.1.2");

or whatever it is IP.

S.

+1
source

, INADDR_ANY . INADDR_ANY , IP, ( ). . inet_pton IP- .

+1

Source: https://habr.com/ru/post/1730434/


All Articles