iPhone 上的本地与远程套接字连接

发布于 2024-08-19 04:30:55 字数 2744 浏览 3 评论 0原文

我想通过创建并连接到 udp 套接字将 OSC 消息从 iphone 发送到另一个程序 (max/msp)。这在 iPhone 模拟器上有效,即当两个应用程序在同一台计算机上运行时,但当我在手机本身上安装应用程序时则无效。

我认为问题可能出在指定远程计算机的 IP 上。我使用 sockaddr_in 结构来指定 IP 和端口信息。当我在模拟器中运行代码时,可以将 IP 指定为 INADDR_ANY:

sin_addr.s_addr = INADDR_ANY;

当我在设备上运行它时,我试图将我的 IP 转换为十六进制数字并指定它而不是 INADDR_ANY。这对于模拟器或设备都不起作用。

控制台显示套接字正在连接并发送数据,但远程程序(max/msp)根本没有接收任何数据。

我尝试导入正确的框架,以便它可以在设备和模拟器上运行。

完整代码如下:

import "UDPSocketCreate.h"

@implementation UDPSocketCreate

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

//addr 是 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;

// 此方法在初始化后从应用程序委托调用

-(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

任何帮助将不胜感激

I want to send OSC messages from iphone to another programme (max/msp) by creating and connecting to a udp socket. this works from the iphone simulator, i.e. when both apps are running on the same computer but not when i install the app on the phone itself.

I think the problem could be with specifying the IP of the remote computer. I am using the sockaddr_in struct to specify IP and port info. when i run the code in the simulator it is fine to specify the IP as INADDR_ANY:

sin_addr.s_addr = INADDR_ANY;

when i run it on the device i'm trying to convert my IP into a hexidecimal number and specifying that instead of INADDR_ANY. This doesn't work for either the simulator or the device.

The console shows that the the socket is connecting and sending data fine but the remote programme (max/msp) doesn't receive any data at all.

I have tried importing the right frameworks so that it should work on both device and simulator.

the 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 app 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遇到 2024-08-26 04:30:55

改变;

in_addr_t myAddress = 0xC0A80145

或无论

in_addr_t myAddress = inet_addr("192.168.1.2");

该 IP 是什么。

S。

Change;

in_addr_t myAddress = 0xC0A80145

to

in_addr_t myAddress = inet_addr("192.168.1.2");

or whatever that IP is.

S.

━╋う一瞬間旳綻放 2024-08-26 04:30:55

除非我误解了您正在尝试使用 INADDR_ANY 作为服务器地址进行连接。 INADDR_ANY 仅用于侦听服务器告诉 IP 堆栈它想要侦听任何 网络接口(而不是多网络上的特定 接口)宿主机。)客户端需要服务器的显式服务器地址来发送数据包。查看inet_pton函数以了解如何将IP地址从字符串转换为网络表示形式。

Unless I misunderstood you are trying to connect with INADDR_ANY as server address. INADDR_ANY is only for listening server to tell the IP stack that it wants to listen on any network interface (versus a specific interface on a multi-homed machine.) The client needs explicit server address of the server to send packets to. Look into inet_pton function for how to convert IP address from character string to network representation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文