title: Hops tracing ttl reciveform on ios
body: <p>I'm trying to implement simple traceroute for the iOS. Everything seems to work fine, except that somehow when I run my application on simulator or on the device it finds only a few (6-7) first routers on the way when the CLI traceroute finds all 14 routers.</p>
  
  <pre><code>const char *c = "www.gmail.com";
  struct hostent *host_entry = gethostbyname(c);
  char *ip_addr;
  ip_addr = inet_ntoa(*((struct in_addr *)host_entry-&gt;h_addr_list[0]));
  struct sockaddr_in destination, fromAddr;
  int recv_sock;
  int send_sock;
  
  // Creting Sockets///
  
  if ((recv_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_ICMP)) &lt;
      0) // using UDP socket.
  {
    NSLog(@"Could not cretae recv_sock.\n");
  }
  if ((send_sock = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) {
    NSLog(@"Could not cretae send_sock.\n");
  }
  memset(&amp;destination, 0, sizeof(destination));
  destination.sin_family = AF_INET;
  destination.sin_addr.s_addr = inet_addr(ip_addr);
  destination.sin_port = htons(80);
  struct timeval tv;
  tv.tv_sec = 0;
  tv.tv_usec = 10000;
  setsockopt(recv_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&amp;tv,
             sizeof(struct timeval));
  char *cmsg = "GET / HTTP/1.1\r\n\r\n";
  int max_ttl = 20;
  int num_attempts = 5;
  socklen_t n = sizeof(fromAddr);
  char buf[100];
  for (int ttl = 1; ttl &lt;= max_ttl; ttl++) {
    memset(&amp;fromAddr, 0, sizeof(fromAddr));
    if (setsockopt(send_sock, IPPROTO_IP, IP_TTL, &amp;ttl, sizeof(ttl)) &lt; 0)
      NSLog(@"error in setsockopt\n");
    for (int try = 0; try &lt; num_attempts; try ++) {
      if (sendto(send_sock, cmsg, sizeof(cmsg), 0,
                 (struct sockaddr *)&amp;destination,
                 sizeof(destination)) != sizeof(cmsg))
        NSLog(@"error in send to...\n@");
      int res = 0;
      if ((res = recvfrom(recv_sock, buf, 100, 0, (struct sockaddr *)&amp;fromAddr,
                          &amp;n)) &lt; 0) {
        NSLog(@"an error: %s; recvfrom returned %d\n", strerror(errno), res);
      } else {
        char display[16] = {0};
        inet_ntop(AF_INET, &amp;fromAddr.sin_addr.s_addr, display, sizeof(display));
        NSLog(@"Received packet from%s for TTL=%d\n", display, ttl);
        break;
      }
    }
  }
  </code></pre>
  
  <p>I have tried to bind the send socket but have same results and I can't use Sock_raw on iOS. I tried to run it on my mac and got same results. The error I get is "Resource temporarily unavailable;" for the <code>recvfrom()</code>. Why is that? How can I fix it?</p>