tags:

views:

502

answers:

1

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:

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

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.

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;

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;

sending packet is like:

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);

and recieving packets is like:

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
}
A: 

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.

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.

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

unwind