在127.0.0.1(localhost)上将数据发送到UDP套接字时,“目标无法访问”(“端口不可达”)

时间:2012-03-25 20:12:29

标签: c sockets

我在C-application中的套接字之间发送数据。我首先绑定'receiver'来侦听127.0.0.1(localhost)上的某个端口(阻塞 - 在一个单独的线程中)。然后我开始向这个端口发送数据。 我从程序中打印出以下内容:

*** Created server socket good. Server socket is 4 
(..)
 port nr 4793 & 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 

使用loopback接口运行Wireshark,我注意到数据包被发送到正确的目标地址。 但是,我收到错误'Destination unreachable'('Port unreachable')'。尽管关闭了防火墙,但我得到了同样的错误。

代码中是否有错误;我该如何解决这个问题? 以下是代码摘录:

header-file中的一些定义:

#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

启动发件人和收件人的程序:

    ret = init_sender();
    if (ret != OF_STATUS_OK) {
        OF_PRINT_ERROR(("ERROR, init_sender() failed\n"))
        goto error;
    }

     // >>> Step #3 <<<
      // 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( &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..

发信人:

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 < 0)
      {
        printf("*** ERROR - socket() failed \n");
        exit(-1);
      }
        printf("*** Sender -- socket created \n");

            printf("*** Start sending messages.. \n");   



  // >>> Step #2 <<<
      // 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 < 10 ; j++ ) 
       {
       retcode = sendto(client_s, (const void*) encoding_symbols_tab[j],  symbol_size, 0,
      (struct sockaddr *)&server_addr, sizeof(server_addr) );

      if (retcode < 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 < 0)
  {
    printf("*** ERROR - close() failed \n");
    exit(-1);

接收器:

    init_receive () {


          // >>> Step #1 <<<
          // 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 < 0)
          {
            printf("*** ERROR - socket() failed \n");
            exit(-1);
          }
          else
            printf("*** Created server socket good. Server socket is %d \n", server_s);


          // >>> Step #2 <<<
          // 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 & ip %s", PORT_NUM, IP_ADDR);

          retcode = bind(server_s, (struct sockaddr *)&server_addr,
            sizeof(server_addr));

          if (retcode < 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 <  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) < 0) {
  //  printf ("error for the moment");
//}

          retcode = recvfrom(server_s, in_buf, sizeof(in_buf), 0,
            (struct sockaddr *)&client_addr, &addr_len);

          if (retcode < 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 < 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

}



}

1 个答案:

答案 0 :(得分:3)

这一行(在发件人中):

server_addr.sin_addr.s_addr = inet_addr(IP_ADDR);

是对的。

这一行(在接收者中):

server_addr.sin_addr.s_addr = htonl(IP_ADDR);

错了。它有点令人惊讶,甚至干净地编译(或做到了吗?)。