1

我被我的 UDP talker 应用程序困住了。目前的目标是初始化服务器,注册一个客户端,然后继续向该客户端发送一些东西。我已经通过 Beej 的网络指南工作并编写了以下库实现:

这初始化服务器

int init_udp_server(const char *port_string){

  /** Check the input data **/
  if(port_string == NULL)
    port_string = DEFAULT_PORT;

  /** Get the information for the server **/
  memset(&addrinfo_hints, 0, sizeof addrinfo_hints);
  /* Use either protocol (v4, v6) */
  addrinfo_hints.ai_family = AF_UNSPEC;
  /* Use UDP socket type */
  addrinfo_hints.ai_socktype = SOCK_DGRAM;
  /* Use system IP */
  addrinfo_hints.ai_flags = AI_PASSIVE;

  if( (ret = getaddrinfo(NULL, port_string, &addrinfo_hints, &addrinfo_server)) 
      != 0 ){
    printf("Server:getaddrinfo: %s\n", gai_strerror(ret));  
    return -1;
  }

  /** Loop through the list returned by getaddrinfo and get socket **/
  for( addrinfo_queue = addrinfo_server; addrinfo_queue != NULL; 
      addrinfo_queue = addrinfo_queue->ai_next){
    if((sockfd = socket(addrinfo_queue->ai_family,
            addrinfo_queue->ai_socktype, addrinfo_queue->ai_protocol)) == -1){
      error("Server: get socket failed");
      continue;
    }
    if(bind(sockfd, addrinfo_queue->ai_addr, addrinfo_queue->ai_addrlen)
        == -1){
      close(sockfd);
      error("Server: Bind to socket error");
      continue;
    }
    break;

  }
  /* If we got to addrinfo_queue == NULL, we did not get a valid socket */
  if(addrinfo_queue == NULL){
    error("Server: Could not bind a socket");
    return -1;
  }
  /* We do not need the addrinfo_server anymore */
  freeaddrinfo(addrinfo_server);
  return 0;
}

这注册了客户端

int udp_server_setup_client(const char *client_addr, const char *port_string, int     client_nr){


  /** Check the input data **/
  if(port_string == NULL)
    port_string = DEFAULT_PORT;
  if(client_addr == NULL){
    error("No valid client list");
    return -1;
  }
  if(client_nr < 0 || client_nr > 7){
    error("No valid client Nr.");
    return -1;
  } 

  memset(&addrinfo_hints, 0, sizeof addrinfo_hints);
  /* Use either protocol (v4, v6) */
  addrinfo_hints.ai_family = AF_UNSPEC;
  /* Use UDP socket type */
  addrinfo_hints.ai_socktype = SOCK_DGRAM;

  /* Get the information for the client */
  if( (ret = getaddrinfo( client_addr, port_string, &addrinfo_hints,
          &current)) != 0 ){
    printf("Client:getaddrinfo: %s\n", gai_strerror(ret));  
    return -1;
  }
  else{
    /* We read out the IP, kind of a nice check to see wheter all went fine */
    char ip4[INET_ADDRSTRLEN];
    struct sockaddr_in *sa = (struct sockaddr_in*) current->ai_addr;
    inet_ntop(AF_INET, &(sa->sin_addr),ip4, INET_ADDRSTRLEN);
    printf("Clients address: %s\n",ip4);
    addrinfo_clients[client_nr] = current;
  }
  return 0;
}

最后这是为了写作

int udp_server_write(const char *buffer, int buffer_size, int client_nr){
  /* Sanity check of the input */
  if(client_nr > (MAX_NR_CLIENTS - 1) || client_nr < 0){
    error("Not a valid client");
    return -1;
  }
  if(buffer == NULL){
    error("Not a valid buffer address");
    return -1;
  }
  /* Just so we type less */
  current = addrinfo_clients[client_nr];

  socklen = sizeof current->ai_addr; 
  if((ret = sendto(sockfd, (void*)buffer, buffer_size, 0,
        (sockaddr*)current->ai_addr, socklen)) == -1){
    printf("Failed to send message to client %i\n", client_nr);
    printf("Error Code: %s\n",gai_strerror(ret)); 
    return -1;    
    }
  else if(ret < buffer_size){
    printf("Wrote only %i of %i bytes\n", ret, buffer_size);
    return -1;
  }
  return ret;
}

我这样调用函数

init_udp_server("3334"); 

udp_server_setup_client("192.168.1.5", "3334", 0);

udp_server_write(send_buf, 256, 0);

一旦调用sendto()就报错:Failed to send message to client 0 Error Code: Bad value for ai_flags 我用gdb查了一下,发现addrinfo结构体填写正确,客户端地址有效. 有人知道在哪里看吗?我的想法已经不多了...

谢谢,文茨勒恩

4

1 回答 1

3

调用sendto()时,最后一个参数被设置为sizeof current->ai_addr,这是错误的。current->ai_addr被定义为一个sockaddr*指针,所以sizeof current->ai_addr在 32 位系统上总是返回 4,在 64 位系统上总是返回 8。碰巧 IPv4 地址的大小为 4 个字节,因此sizeof current->ai_addr仅适用于 32 位系统上的 IPv4 地址,但对于 32 位系统上的 IPv6 地址和 64 位系统上的所有地址总是会失败。您需要使用current->ai_addrlen而不是sizeof.

此外,将 -1 传递给gai_strerror()无效。它希望您传入一个真实的错误代码,例如getaddrinfo()and的返回值getnameinfo()sendto()不返回实际的错误代码。当它失败时,您必须WSAGetLastError()在 Windows 或errno其他系统上使用才能获取实际的错误代码。

试试这个:

if ((ret = sendto(sockfd, (char*)buffer, buffer_size, 0, (sockaddr*)current->ai_addr, current->ai_addrlen)) == -1)
{
    #ifdef _WIN32
    ret = WSAGetLastError();
    #else
    ret = errno;
    #endif

    printf("Failed to send message to client %i\n", client_nr);
    printf("Error Code: (%d) %s\n", ret, gai_strerror(ret)); 
    return -1;    
}
于 2013-01-02T20:10:58.687 回答