views:

381

answers:

1

I'm trying to establish UDP communication between a MAC OS and an iPod through Wi-Fi, at this point I'm able to send packets from the iPod and I can see those packets have the right MAC and ip addresses (I'm using wireshark to monitor the network) but the MAC receives the packets only when the wireshark is on, otherwise recvfrom() returns -1.

When I try to transmit from MAC to iPhone I have the same result, I can see the packets are sent but the iPhone doesn't seem to get them.

I'm using the next code to send:

 struct addrinfo hints; 
 int rv; 

 memset(&hints, 0, sizeof hints); 
 hints.ai_family = AF_UNSPEC; 
 hints.ai_socktype = SOCK_DGRAM;

 if ((rv = getaddrinfo(IP, SERVERPORT, &hints, &servinfo)) != 0) { 
  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); 
  return 1;
 }
 // loop through all the results and make a socket 
 for(p = servinfo; p != NULL; p = p->ai_next) {
  if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { 
   perror("talker: socket"); 
   continue;
  }   
  break;
 }

 if (p == NULL) { 
  fprintf(stderr, "talker: failed to bind socket\n"); 
  return 2;
 }
 while (cond)
  sntBytes += sendto(sockfd, message, strlen(message), 0, p->ai_addr, p->ai_addrlen); 
 return 0;

and this code to receive:

struct addrinfo hints, *p; 
 int rv;

 memset(&hints, 0, sizeof hints); 
 hints.ai_family = AF_UNSPEC; // set 
 hints.ai_socktype = SOCK_DGRAM; 
 hints.ai_flags = AI_PASSIVE; // use to AF_INET to force IPv4 my IP 

 if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
  fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); 
  return 1;
 }
 // loop through all the results and bind to the first we can 
 for(p = servinfo; p != NULL; p = p->ai_next) {
  if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
   perror("listener: socket"); 
   continue;
  }  


  if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { 
   close(sockfd);
   perror("listener: bind"); 
   continue;
  } 

  break;
 }

 if (p == NULL) { 
  fprintf(stderr, "listener: failed to bind socket\n"); 
  return 2;
 } 

 addr_len = sizeof their_addr; 
 fcntl(sockfd, F_SETFL,O_NONBLOCK);

 int rcvbuf_size = 128 * 1024; // That's 128Kb of buffer space.
 setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, 
       &rcvbuf_size, sizeof(rcvbuf_size));

 printf("listener: waiting to recvfrom...\n");

 while (cond)
    rcvBytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0, (struct sockaddr *)&their_addr, &addr_len);

return 0;

What am I missing?

A: 

It would be good to get some more information about the length of data you are sending. I will assume you are trying to send an ASCII string.

Also, this appears to be either never called or an infinite send loop:

while (cond)
  sntBytes += sendto(sockfd, message, strlen(message), 0, p->ai_addr, p->ai_addrlen);

You might want to use code that actually includes some error checking:

Send String

int sendResult = send( connectedSocket, stringBuffer, stringLength, 0 );
    if (sendResult == -1)  {
        perror("Error while trying to send string!");
    }
    else {
        NSLog(@"String '%s' sent successfully", stringBuffer );
    }

Receive String

memset( ReceiveBuffer, '\0', sizeof(ReceiveBuffer) );
    int receiveResult = recv( connectedSocket, ReceiveBuffer, sizeof(ReceiveBuffer), 0);
    if ( receiveResult == -1 ) {
        perror("recv");
    }
    else {
        NSLog(@"String received successfully: '%s'", ReceiveBuffer );
    }
Brock Woolf
Thank you for your response. The loops are just for demonstration of the sending command in the actual code I'm using a timer sending/receiving a packet every 0.1 second. As for the data I'm trying to send, right now it's ASCII string but eventually it will be RTP packets.
Eli
You ask for help with specific code, but then show pseudocode code in its place? Nonsensical!
Brock Woolf