title: 'Destination unreachable' ('Port unreachable') when sending data to UDP-socket on 127.0.0.1 (localhost)
body: <p>I send data between sockets in a C-application. I first bind the 'receiver' to listen to a certain port at 127.0.0.1 (localhost) (blocking - in a separate thread). I then start sending data to this port.
  I get the following print-out from the program:</p>
  
  <pre><code>*** Created server socket good. Server socket is 4 
  (..)
   port nr 4793 &amp; ip 127.0.0.1***Bind succeed.. 
  Waiting for recvfrom() to complete... 
  waiting to receive msg*** Start sending messages.. 
  sendto() successs 
  sendto() successs 
  (..)
  sendto() successs 
  </code></pre>
  
  <p>Running Wireshark with the loopback-interface, I notice that the packets are sent to the correct destination address.
  However, I get the error 'Destination unreachable' ('Port unreachable')'. I get the same error despite turning off the firewall.</p>
  
  <p>Is there an error in the code; and how do I resolve this?
  Below follows excerpts from the code:</p>
  
  <p>Some definitions in header-file:</p>
  
  <pre><code>#define  PORT_NUM   4793    // Arbitrary port number for the server
  #define  IP_ADDR      "127.0.0.1" // IP address of server1 (*** HARDWIRED ***)
  #define  SYMBOL_SIZE 1024 //todo..on a different position also
  </code></pre>
  
  <p>Program initiating sender and receiver:</p>
  
  <pre><code>    ret = init_sender();
      if (ret != OF_STATUS_OK) {
          OF_PRINT_ERROR(("ERROR, init_sender() failed\n"))
          goto error;
      }
  
       // &gt;&gt;&gt; Step #3 &lt;&lt;&lt;
        // Wait to receive a message from client..Since receiver is waiting-do this in separate thread.
      pthread_t thread1;
      int  iret1;
      char *message1 = "Thread 1";
  
      iret1 = pthread_create( &amp;thread1, NULL, init_receiver, (void*) message1);
  
      ret = encode(); //this sends messages
      if (ret != OF_STATUS_OK) {
          OF_PRINT_ERROR(("ERROR, encode() failed\n"))
          goto error;
      }
  
  
      if (ret != OF_STATUS_OK) {
          OF_PRINT_ERROR(("ERROR, init_receiver() failed\n"))
          goto error;
      } //todo..ok after init_receiver?
      ret = init_tx_simulator();  /* must be done after init_sender */
      if (ret != OF_STATUS_OK) {
          OF_PRINT_ERROR(("ERROR, init_tx_simulator() failed\n"))
          goto error;
      }
  
      print_params();
      //print_rx_stats();
  #endif /* OF_DEBUG */
  #endif
  
      pthread_join (thread1, NULL); //We want the receiver thread to be finished before finishing it..
  </code></pre>
  
  <p>Sender:</p>
  
  <pre><code>of_status_t encode (void)
  
  {
  
        // Create a socket
        //   - AF_INET is Address Family Internet and SOCK_DGRAM is datagram
        client_s = socket(AF_INET, SOCK_DGRAM, 0);
        if (client_s &lt; 0)
        {
          printf("*** ERROR - socket() failed \n");
          exit(-1);
        }
          printf("*** Sender -- socket created \n");
  
              printf("*** Start sending messages.. \n");   
  
  
  
    // &gt;&gt;&gt; Step #2 &lt;&lt;&lt;
        // Fill-in server1 socket's address information
        server_addr.sin_family = AF_INET;                 // Address family to use
        server_addr.sin_port = htons(PORT_NUM);           // Port num to use
        server_addr.sin_addr.s_addr = inet_addr(IP_ADDR); // IP address to use 
  
  
     //n is number of symbols to sent -- repair + original..
  int j= 0;
     for (j=0; j &lt; 10 ; j++ ) 
         {
         retcode = sendto(client_s, (const void*) encoding_symbols_tab[j],  symbol_size, 0,
        (struct sockaddr *)&amp;server_addr, sizeof(server_addr) );
  
        if (retcode &lt; 0)
        {
         // printf("*** ERROR - sendto() failed.. retcode is %d  \n", retcode);
          printf ("errno is %s",strerror (errno));        
          exit(-1);
        }
        else
         printf("sendto() successs \n");
  
         }
  
   retcode = close(client_s);
    if (retcode &lt; 0)
    {
      printf("*** ERROR - close() failed \n");
      exit(-1);
  </code></pre>
  
  <p>Receiver:</p>
  
  <pre><code>    init_receive () {
  
  
            // &gt;&gt;&gt; Step #1 &lt;&lt;&lt;
            // Create a socket
            //   - AF_INET is Address Family Internet and SOCK_DGRAM is datagram
            server_s = socket(AF_INET, SOCK_DGRAM, 0);
            if (server_s &lt; 0)
            {
              printf("*** ERROR - socket() failed \n");
              exit(-1);
            }
            else
              printf("*** Created server socket good. Server socket is %d \n", server_s);
  
  
            // &gt;&gt;&gt; Step #2 &lt;&lt;&lt;
            // Fill-in my socket's address information
            server_addr.sin_family = AF_INET;                 // Address family to use
            server_addr.sin_port = htons(PORT_NUM);           // Port number to use
            server_addr.sin_addr.s_addr = htonl(IP_ADDR);  // Listen on this IP..
              printf("***We have a server socket; and now we will try to bind it with the IP_ADDR-local host -- that we sent.. \n port nr %d &amp; ip %s", PORT_NUM, IP_ADDR);
  
            retcode = bind(server_s, (struct sockaddr *)&amp;server_addr,
              sizeof(server_addr));
  
            if (retcode &lt; 0)
            {
              printf("*** ERROR - bind() failed \n");
              exit(-1);
            }
          else
              printf("***Bind succeed.. \n");
  
  
          waitToReceiveMessageFromClient (); //call it and wait to receive messages.
  
          return OF_STATUS_OK;
  
      no_mem:
          return OF_STATUS_ERROR;
      }
  
  waitToReeiveMessageFromClient () {
  
  int
  waitToReceiveMessageFromClient ( void )
  {
        printf("Waiting for recvfrom() to complete... \n");
        addr_len = sizeof(client_addr);
       encoding_symbols_tab = (void*) calloc(MAX_N, sizeof(void*));
      int i = 0;
      //Run until we have finished all messages from the Sender.
  
        while (numberMessagesReceivedCounter &lt;  tot_nb_source_symbols + tot_nb_repair_symbols )
          {
          printf ("waiting to receive msg");
  
          encoding_symbols_tab[i] = (void*)calloc(1, SYMBOL_SIZE); //each
  //change to non-blocking
  //if(fcntl(server_s, F_SETFL, fcntl(server_s, F_GETFL) | O_NONBLOCK) &lt; 0) {
    //  printf ("error for the moment");
  //}
  
            retcode = recvfrom(server_s, in_buf, sizeof(in_buf), 0,
              (struct sockaddr *)&amp;client_addr, &amp;addr_len);
  
            if (retcode &lt; 0)
            {
              fprintf (stderr,"errno is %d", errno);
              exit(-1);
            }
            else
              { //copy all symbols to its right location from received message.
          printf("*** Received message.. \n");
  
              int k= 0;
              for (k= 0; k &lt; sizeof (in_buf) ; k++ )
              {
              encoding_symbols_tab[numberMessagesReceivedCounter][k] = (char*) in_buf[k];
              }
              numberMessagesReceivedCounter++;
  
              //Finished ready to do decoding.
              if (numberMessagesReceivedCounter ==  tot_nb_source_symbols + tot_nb_repair_symbols )
               {
              receive_and_decode ();
              break;
              }
  
              }
          }
       //add to buffer... numberMessagesReceivedCounter
  
  }
  
  
  
  }
  </code></pre>