当对recvfrom检索到的sockaddr *进行sendto-call时,“地址错误”

时间:2012-05-30 15:13:18

标签: c++ sockets

    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时,错误&#39; 9errno是错误的地址&#39;打印出来。 造成这种情况的原因是什么?如何解决?

在函数recvfrom中从发送方接收数据,addrSenderOfVideo包含端口和ip-number。 发件人有ip&#39; 127.0.0.1&#39;这也是addrSenderOfVideo中包含的IP。

1 个答案:

答案 0 :(得分:2)

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