C的sockaddr函数调用sendto?

时间:2017-08-25 21:08:23

标签: c linux sockets pointers sockaddr-in

我目前正在为学校作业设置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");
  }

如果编译了程序,我得到警告:在SendData中的sendto功能调用上传递来自不兼容指针类型的“recvfrom”的参数5。。 我的主程序中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);
    }
  }
}

2 个答案:

答案 0 :(得分:2)

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

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

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

您未能在destAddrUdpSendData中考虑不同类型的main。对sendto的正确调用将是:

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

答案 1 :(得分:0)

问题在于sizeof(destAddrUdp)(struct sockaddr*) &destAddrUdp

在您的主要内容中,destAddrUdpstruct sockaddr_in,在您的函数中,它是sockaddr_in*,因此您无法平等地使用它们。首先,函数中的sizeof(destAddrUdp)将为您指定指针的大小,其次,函数中的&destAddrUdp将为您提供destAddrUdp**。那不是你想要的。

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