0
    struct sockaddr_in  addrSenderOfVideo
    struct sockaddr_in   client_addr;     // Client Internet address
    char* buffer = new char[2];
    int sizeOfBuffer=1050;
    int clientfd;
    char* bufferWithPacketData = new char[sizeOfBuffer];
    extern int                  client_s;        // Client socket 

    if (client_s < 0)
          {
            printf("*** ERROR - socket() failed \n");
          }

           //Client address.
      client_addr.sin_family = AF_INET;
      client_addr.sin_addr.s_addr = htonl(INADDR_ANY); // IP address to use
      client_addr.sin_port = htons(portNumClient); 

    clientfd = bind(client_s,(const sockaddr*) &client_addr, sizeof(client_addr));
    fprintf (stderr, "inside of listen for new packet<; clientfd is %d",clientfd);

      if (clientfd < 0) 
        {
        fprintf (stderr, "WARNING -1 doing BIND!");

        }


    int n = recvfrom (client_s, bufferWithPacketData, sizeOfBuffer, 0,(struct sockaddr*)&addrSenderOfVideo, &fromlen);

   retcode = sendto(client_s, buffer,  symbol_size, 0,
          (struct sockaddr *)&addrSenderOfVideo, sizeof(addrSenderOfVideo) );

调用 sendto 时,会打印出错误“9errno is Bad address”。这是什么原因,我该如何解决?

数据在函数 recvfrom 中从发送方接收,并addrSenderOfVideo包含端口和 ip 号。发件人的 ip 为“127.0.0.1”,这也是包含在addrSenderOfVideo.

4

1 回答 1

2

其中有两个参数sendto是指向用户空间缓冲区的指针,因此可以触发EFAULT:数据缓冲区和地址。该地址指向一个固定大小的局部变量,所以它似乎不是罪魁祸首。所以它必须是数据缓冲区。数据缓冲区指向一个大小为 2 的数组。您没有向我们展示您是如何初始化symbol_size的。概率是否symbol_size大于 2?

于 2012-05-30T20:02:02.947 回答