使用<代码>sendto()和recv from (
,通过UDP向WFI发送一些简单的包装。
我用两部电话和一台模拟器进行抽查,结果为:
Packets sent from phones -> recieved by simulator Packets sent from simulator -> simulator recvfrom remains blocking. Packets sent from phones -> other phone recvfrom remains blocking.
我不敢肯定如何开始分解,因为模拟器/磁盘能够接收包装单,但电话似乎不会收到电文。
稍微例外,我是否需要把我的包裹放在我网络的MTU下? 还是由监督厅或其他一些低级软件处理碎块问题?
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;
(a) 证明书数据为:
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
}