2

我目前正在为学校作业设置 UDP 套接字,但我不知道如何正确地将 sockaddr 作为参数发送到函数中。SendData 的输入参数的定义方式与它们在 sendto 函数中的定义方式相同。

void SendData(int fileDescriptor, const struct sockaddr_in *destAddrUdp, unsigned char buffer[MAXMSG])
{
  /* crc */
  int strlength = strlen(buffer);
  unsigned char SCRC = CRC(buffer, strlength-1);
  buffer[strlength-1] = SCRC;
  buffer[strlength] = '\0'; 
  /* send */
  if (sendto(fileDescriptor, buffer, strlen(buffer), 0, (struct sockaddr*) &destAddrUdp,sizeof(destAddrUdp)) < 0) {
    fprintf(stderr, "Could not send data\n");
  }
} `

当从 main 调用下面的代码时,它可以工作,但是当从 SendData 调用它时,sendto 的返回值为-1。

if (sendto(fileDescriptor, buffer, strlen(buffer), 0, (struct sockaddr*) &destAddrUdp,sizeof(destAddrUdp)) < 0) {
    fprintf(stderr, "Could not send data\n");
  }

如果程序已编译,我会收到警告:从不兼容的指针类型传递“recvfrom”的参数 5。在 SendData 中的 sendto 函数调用上。我的主程序中对 SendData 的函数调用是:SendData(sockfd, (struct sockaddr *) &destAddrUdp, buffer);

相关的主程序:

unsigned char SCRC;
  unsigned char strlength;
  unsigned char buffer[MAXMSG-5];
  unsigned char header[MAXMSG];
  struct rtp_struct *sendstruct; 
  /* option 2 */
  struct sockaddr_in destAddrUdp;
  int sockfd; 
  char dstHost[15]; 
  printf("input host IP:\n>");
  fgets(dstHost, 15, stdin);

  /* Create socket */ 
  if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 
    fprintf(stderr, "Can't create UDP socket\n");

  /* set configurations */
  memset (&destAddrUdp, 0, sizeof(destAddrUdp));  //set zero
  destAddrUdp.sin_family = AF_INET; //internet use
  destAddrUdp.sin_addr.s_addr = inet_addr(dstHost); //set so anyone can connect
  destAddrUdp.sin_port = htons(dstUdpPort); //set the port to use

  /* Generate CRC table */
  GenerateCRCTable();

/* get string and send */
while(1) {
    printf("\n>");
    fgets(buffer, MAXMSG, stdin);
    if(strncmp(buffer,"quit\n",MAXMSG) != 0){ //if read string == quit, see else
      strlength = strlen(buffer);
      SCRC = CRC(buffer, strlength-1);
      buffer[strlength-1] = SCRC;
      buffer[strlength] = '\0';

      SendData(sockfd, (struct sockaddr *) &destAddrUdp, buffer);
      /*if (sendto(sockfd, buffer, strlength, 0, (struct sockaddr *) &destAddrUdp,sizeof(destAddrUdp)) < 0){
        fprintf(stderr, "Could not send data\n");}*/
    }  
    else {   
      close(sockfd); 
      exit(EXIT_SUCCESS);
    }
  }
}
4

2 回答 2

2

在您的SendData函数中,参数destAddrUdp的类型为const struct sockaddr_in *。然后,当您拨打此电话时:

sendto(fileDescriptor, buffer, strlen(buffer), 0, 
       (struct sockaddr*) &destAddrUdp, sizeof(destAddrUdp)

表达式&destAddrUdp具有 type const struct sockaddr_in **,因此指针类型不兼容。此外,sizeof(destAddrUdp)返回的是指针的大小,而不是结构的大小。

您没有考虑destAddrUdpinSendData和的不同类型main。正确的调用sendto是:

sendto(fileDescriptor, buffer, strlen(buffer), 0, 
       (struct sockaddr*) destAddrUdp, sizeof(*destAddrUdp)
于 2017-08-25T21:18:05.253 回答
0

问题是sizeof(destAddrUdp)(struct sockaddr*) &destAddrUdp

在你的 main 中,destAddrUdp是 a struct sockaddr_in,但是在你的函数中,它是 a sockaddr_in*,所以你不能平等地使用它们。首先,sizeof(destAddrUdp)在你的函数中会给你一个指针的大小,其次,&destAddrUdp在你的函数中会给你一个destAddrUdp**. 那不是你想要的。

尝试sendto(fileDescriptor, buffer, strlen(buffer), 0, (struct sockaddr*) destAddrUdp,sizeof(*destAddrUdp))

于 2017-08-25T21:15:39.473 回答