iPhone接收UDP数据包的问题 [英] iphone problem receiving UDP packets

查看:177
本文介绍了iPhone接收UDP数据包的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 sendto() recvfrom()通过WiFI上的UDP发送一些简单的数据包。

I'm using sendto() and recvfrom() to send some simple packets via UDP over WiFI.

我尝试过使用两部手机和一台模拟器,我得到的结果是:

I've tried using two phones, and a simulator, the results I'm getting are:

从手机发送的数据包 - >由模拟器收到的
从模拟器发送的数据包 - >模拟器recvfrom仍然阻塞。
从手机发送的数据包 - >其他手机recvfrom仍然阻止。

Packets sent from phones -> recieved by simulator Packets sent from simulator -> simulator recvfrom remains blocking. Packets sent from phones -> other phone recvfrom remains blocking.

我不知道如何开始调试这个,因为模拟器/ mac能够接收数据包,但电话似乎没有收到消息。

I'm not sure how to start debugging this one, since the simulator/mac is able to receive the the packets, but the phones don't appear to be getting the message.

稍微一点,我是否需要将我的数据包保持在MTU以下网络?或者操作系统或其他一些较低级别的软件处理碎片?

A slight aside, do I need to keep my packets below the MTU for my network? Or is fragmentation handled by the OS or some other lower level software?

更新:
我忘了包含数据包的大小和结构。我正在传输:

UPDATE: I forgot to include the packet size and structure. I'm transmitting:

typedef struct PacketForTransmission {
    int32_t packetTypeIdentifier;
    char data[64];  // size to fit my biggest struct
} PacketForTransmission;

其中char数据[64]为:

of which the char data[64] is:

typedef struct PacketHeader{ 
    uint32_t identifier; 
    uint32_t datatype; 
} PacketHeader; 

typedef struct BasePacket{ 
    PacketHeader header; 
    int32_t cardValue;
    char sendingDeviceID[41]; //dont forget to save room for the NULL terminator!
} BasePacket;

typedef struct PositionPacket{ 
    BasePacket basePacket;
    int32_t x; 
    int32_t y; 
} PositionPacket;

发送包就像:

PositionPacket packet; 
bzero(&packet, sizeof(packet));
//fill packet with it's associated data

PacketForTransmission transmissionPacket;
transmissionPacket.packetTypeIdentifier = kPositionPacketType;
memcpy(&transmissionPacket.data, (void*)&packet, sizeof(packet));  //put the PositionPacket into data[64]

size_t sendResult = sendto(_socket, &transmissionPacket, sizeof(transmissionPacket), 0, [address bytes], [address length]); 
NSLog(@"packet sent of size: %i", sendResult);

并收到包裹如下:

while(1){ 
    char dataBuffer[8192];
        struct sockaddr addr; 
     socklen_t socklen = sizeof(addr); 
    ssize_t len = recvfrom(_socket, dataBuffer, sizeof(dataBuffer), 0, &addr,  &socklen);    //continues blocking here
        NSLog(@"packet recieved of length: %i", len);  

    //do some more stuff
}


推荐答案

我不认为你可以在不让操作系统知道你正在收听哪个端口的情况下接收数据。通常将传入数据包的端口号与进程绑定的端口号进行比较,以确定数据包的传输位置。

I don't think you can receive data without letting the OS know which port you're listening to. The port number of the incoming packet is generally compared against which port number(s) a process is bound to, in order to determine where the packet needs to be delivered.

尝试建立一个固定的端口号(只需选择一个随机的16位无符号整数,如49153或其他)并在两侧使用它。

Try establishing a fixed port-number (just pick a random 16-bit unsigned integer, such as 49153 or whatever) and using that on both sides.

你提到使用Bonjour ,我理解允许动态发现网络资源,但我认为这值得尝试。

You mention using Bonjour, which I understand allows dynamic discovery of network resources, but still I think this would be worth trying.

这篇关于iPhone接收UDP数据包的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆