recv()错误C / C ++套接字编程

时间:2013-06-13 10:36:29

标签: c linux sockets select tcp

我正在为TR-069服务器编写CWMP客户端。我的客户代码基于此主题(该主题中还有源代码链接)

CWMP CPE (Client) implementation

我遇到了一个奇怪的问题,即recv()没有读取发送的数据!我已经检查了我发送的数据,这是正确的。我在服务器端安装了Wireshark,它使用发送的相同数据客户端捕获数据包。我正在深入研究TR-069 ACS源代码,我试图将监听套接字模式改为NON_BLOCK - 没用。

套接字定义来源:

int make_socket(unsigned int port)
{
  int sock;
  struct sockaddr_in name;
  sock = socket(PF_INET, SOCK_STREAM, 0);
  if (sock < 0)
  {
    logtr("ACS Server:", "Can not create socket", ERROR, conferr);
    exit(EXIT_FAILURE);
  }
  name.sin_family = AF_INET;
  name.sin_port = htons(port);
  name.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0)
  {
    // perror ("bind");
    logtr("ACS Server:", "Can not bind ", ERROR, conferr);
    exit(EXIT_FAILURE);
  }
  return sock;
}

处理:

void connection(int sock)
{
  if (listen(sock, 10) < 0)
  {
    //perror ("listen");
    logtr("ACS Server:", "Can not listen socket", ERROR, conferr);
    exit(EXIT_FAILURE);
  }

  FD_ZERO(&active_fd_set);
  FD_SET(sock, &active_fd_set);

  //  int status = 3;

  while (1)
  {
    timeout.tv_sec = 10;
    timeout.tv_usec = 0;

    read_fd_set = active_fd_set;
    if (select(FD_SETSIZE + 1, &read_fd_set, NULL, NULL, &timeout) < 0)
    {
      //perror ("Select");
      logtr("ACS Server:", "Can not select socket", ERROR, conferr);
      exit(EXIT_FAILURE);
    }
    for (i = 0; i <= FD_SETSIZE; ++i)
      if (FD_ISSET(i, &read_fd_set))
      {
        if (i == sock)
        {
          size = sizeof(clientname);
          newfd1 = accept(sock, (struct sockaddr *) &clientname, &size);
          if (newfd1 < 0)
          {
            //perror ("accept");
            logtr("ACS Server:", "Can not accept socket", ERROR, conferr);
            exit(EXIT_FAILURE);
          }
          /*
           fprintf (stderr,"Server: connect from host %s, port %i\n",
           (char*)inet_ntoa(clientname.sin_addr),
           ntohs(clientname.sin_port));
           */
          char info[128];
          sprintf(info, "Server: connect from host %s, port %i\n",
              (char*) inet_ntoa(clientname.sin_addr),
              ntohs(clientname.sin_port));
          trace(info);

          FD_SET(newfd1, &active_fd_set);

          memset(fileCBuff[i].fileContBuff, 0,
              sizeof(fileCBuff[i].fileContBuff));
          out_get_param_value0 = 0;
          out_get_param_value1 = 0;
          c_reboot = 0;
          c_download = 0;
          noSqlError = 0;

        }
        else
        {
          send_recive_socket(i);
        }
      }
  }
}

最后失败的地方:

void send_recive_socket( int i )
{
  if( read_from_client(i) < 0 )
  {
    CLOSECONNECTION: 
    close(i);
    FD_CLR (i,&active_fd_set);
//printf("\nClose connection ......\n");\   
  }
  else 
  {  
    ...

int read_from_client( int fileDesc )
{
  int nbytes;
  memset( buffer,0,READ_MSG_SIZE );
  nbytes = recv(fileDesc, buffer, READ_MSG_SIZE,0);//reade data from client
  if( nbytes <= 0 )
  {
    logtr("ACS Server:","Wait Data from TR069 Client ", ERROR, conferr);
   // printf("\nNo data from client");
   // perror ("Read ...");
    return -1;
   }
   else
   {
    ...

我想这就是这个地方,因为每当我停止客户端的执行时,日志都会收到

ACS Server: Wait Data from TR069 Client ERROR servHelper.c read_from_client

提前谢谢。

1 个答案:

答案 0 :(得分:2)

当信号到达时,

recv可能会返回-1errno设置为EINTR。这是一个瞬态故障,并不表示您的套接字连接有任何问题。您可能希望忽略此错误并尝试再次调用recv。在Linux上执行此操作的最简单方法是使用TEMP_FAILURE_RETRY

nbytes = TEMP_FAILURE_RETRY(recv(fileDesc, buffer, READ_MSG_SIZE,0));

这会反复调用recv,直到它返回nbytes==-1 && errno==EINTR

以外的内容