Receiving UDP packets on iPhone

I am trying to establish UDP communication between MAC OS and iPod over Wi-Fi, at the moment I can send packets from the iPod and I can see that these packets have correct MAC addresses and IP addresses (I am using wireshark to monitor the network) , but the MAC only receives packets when wireshark is activated, otherwise recvfrom () returns -1.

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

I am using the following 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 get:

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

+2


a source to share


1 answer


It would be nice to have more information about the length of the data being sent. Let's say you are trying to send ASCII string

.

Also, this appears to be never called or an infinite dispatch 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 );
    }

      

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

      

+1


a source







All Articles