2016-05-10 97 views
2

我正在嘗試創建UDP交互客戶端/服務器。 我在嘗試使用此代碼發送消息時遇到了一些問題。 執行過程中,我得到這個錯誤:協議UDP不支持的地址族 - C

"Address family not supported by protocol" after the sendto function. Thanks in advance!

int prepare_udp_socket(char *pong_addr, char *pong_port) 
{ 
    struct addrinfo gai_hints, *pong_addrinfo = NULL; 
    int ping_socket; 
    int gai_rv; 
    int np; 

      /*** Specify the UDP sockets' options ***/ 
      memset(&gai_hints, 0, sizeof gai_hints); 
      gai_hints.ai_family=AF_INET; 
      gai_hints.ai_socktype=SOCK_DGRAM; 
      gai_hints.ai_flags=0; 
      gai_hints.ai_protocol = IPPROTO_UDP; 

      if ((ping_socket = socket(gai_hints.ai_family, gai_hints.ai_socktype, gai_hints.ai_protocol)) == -1) 
       fail_errno("UDP Ping could not get socket"); 
      /*** change socket behavior to NONBLOCKING ***/ 
      if(fcntl(ping_socket,F_SETFL, O_NONBLOCK)==-1) 
       fail_errno("set NONBLOCKING socket fail"); 

      //call getaddrinfo() in order to get Pong Server address in binary form 
      gai_rv=getaddrinfo(pong_addr, pong_port, &gai_hints, &pong_addrinfo); 

      if(gai_rv!=0){ 
       printf("Getaddrinfo failed\n"); 
       exit(1); 
      } 
      /*** connect the ping_socket UDP socket with the server ***/  
     if(connect(ping_socket, pong_addrinfo->ai_addr, pong_addrinfo->ai_addrlen)!=0){ 
       fail_errno("Can't connect"); 
      }  
      freeaddrinfo(pong_addrinfo); 
      return ping_socket; 
     } 

    double do_ping(int msg_size, int msg_no, char message[msg_size], int ping_socket, double timeout) 
    { 
     int lost_count = 0; 
     char answer_buffer[msg_size]; 
     ssize_t recv_bytes, sent_bytes; 
     int offset; 
     struct sockaddr_storage pong_addr; 
     size_t pong_addr_len = sizeof(struct sockaddr_storage); 
     struct timespec send_time, recv_time; 
     double roundtrip_time_ms; 
     int re_try = 0; 
     /*** write msg_no at the beginning of the message buffer ***/ 
    if(sprintf(message,"%d\n",msg_no)<0){ 
      fail("sprintf(...) failed"); 
     } 
     do { 
      debug(" ... sending message %d\n", msg_no); 
     /*** Store the current time in send_time ***/ 
     if(clock_gettime(CLOCK_MONOTONIC,&send_time)!=0){ 
      fail_errno("clock_gettime failed with sendTime"); 
     } 
     /*** Send the message through the socket ***/ 
     **if((sent_bytes = sendto(ping_socket, &message, msg_size,0,(struct sockaddr*)&pong_addr,pong_addr_len)) == -1){ 
      fail_errno("Cannot send bytes throught the socket");** 
     } 

回答

1

的問題是在你的do_ping功能,sendto期望地址和長度作爲第5和第6的參數,它們是輸入參數,這意味着,您需要指定要發送數據包的目標主機的地址,您必須將地址初始化爲有效的地址。你在做什麼就像'recvfrom`實際上。

另外,message是指向char函數內部的指針,因此請勿在sendto中使用&message,只需傳遞message

struct sockaddr_storage pong_addr; 
// pong_addr is not initialized here, initialize pong_addr to a valid address, like the one you got from `getaddrinfo` call 
size_t pong_addr_len = sizeof(struct sockaddr_storage); 

// then call sendto to send to the address 
if((sent_bytes = sendto(ping_socket, message, msg_size, 0, (struct sockaddr*)&pong_addr, pong_addr_len)) == -1){ 
    fail_errno("Cannot send bytes throught the socket");** 
} 
相關問題