1

我想通过套接字编程读取和写入 Wanpipe 驱动程序(Sangoma 卡的网络设备驱动程序),但我收到此消息错误:“资源暂时不可用”。该卡正在工作,我看到它在 ifconfig 中发送和接收数据包。我已经包含了我的代码,如果有人在这方面帮助我,我会非常高兴。一个相关问题:我将套接字设置为阻塞模式但接收消息没有阻塞?我怎么能阻止recv?

int main(void)
{
   int sd;
   int buflen=WP_HEADER + MAX_PACKET;
   char buf[buflen];
   struct wan_sockaddr_ll sa;
   sd = socket(AF_WANPIPE, SOCK_RAW,0);

   if (sd < 0) /* if socket failed to initialize, exit */
   {
      perror("Error Creating Socket");
      exit(1);
   }

   printf("Socket Descriptor:%d\n",sd);
   memset(&sa,0,sizeof(struct wan_sockaddr_ll));
   strncpy((char*)sa.sll_card,"wanpipe1",sizeof(sa.sl l_card));
   strncpy((char*)sa.sll_device,"w1g1",sizeof(sa.sll_ device)); 
   sa.sll_protocol = htons(PVC_PROT);
   sa.sll_family = AF_WANPIPE;

   if(bind(sd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
   {
      perror("error bind failed");
      close(sd);
      exit(1);
   }

   int data=0;
   int ret=ioctl(sd,FIONBIO,&data);

   if (ret < 0) 
   {
      perror("ioctl error!");
      close(sd);
      return 1;
   }

   fd_set read_fds;
   struct timeval timeout;
   timeout.tv_sec = 10;
   timeout.tv_usec = 0;
   FD_ZERO(&read_fds);
   FD_SET(sd,&read_fds);

   if(select(sd+1, &read_fds, NULL, NULL, &timeout) < 0)
   {
      perror("select() error!");
      exit(1);
   }

   if (FD_ISSET(sd,&read_fds))
      printf("There is data for reading\n");
   else
      printf("There is no data for reading\n");*/

   // MSG_WAITALL | MSG_PEEK | MSG_OOB
   int r=recv(sd,buf,buflen,0);

   if (r < 0) 
   {
      perror("Wanpipe raw socket reading");
      close(sd);
      exit(1);
   }

   printf("\nNumber of bytes read into the buffer: %d",r);
   printf("\nThe read buffer: ");
   puts(buf);
   close(sd);
}

先感谢您。

4

0 回答 0