1

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

CWMP CPE(客户端)实施

我遇到了一个奇怪的问题,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

先感谢您。

4

1 回答 1

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

于 2013-06-13T10:40:16.463 回答