微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法在嵌入式平台上的套接字上运行 fcntl

如何解决无法在嵌入式平台上的套接字上运行 fcntl

我在一个嵌入式平台上工作,但我在处理一些(看似)基本的套接字操作时遇到了问题。 我想打开一个套接字并将其设置为 O_NONBLOCK。我想出了以下示例代码

  #include <stdio.h>
  #include <errno.h>
  #include <string.h>
  #include <unistd.h>
  #include <fcntl.h>
  #include <arpa/inet.h>

  char *host = "10.10.100.1";
  int port = 21;
  int control_fd = -1;
  int code;
  char *hostname;
  static struct sockaddr_in hisctladdr;

char *test(char *host,int port);

char *test(char *host,int port) {
  static char hostnamebuf[256];
  int iSockFlags;

  if (inet_aton(host,&hisctladdr.sin_addr))
  {
    hisctladdr.sin_family = AF_INET;
    hisctladdr.sin_port = htons(port);
    strncpy(hostnamebuf,host,sizeof(hostnamebuf));
    hostnamebuf[sizeof(hostnamebuf) - 1] = 0;
  }
  else
  {
    iprintf("Ftp::test IP dot format required\n");
    code = -1;
    return (NULL);
  }
  hostname = hostnamebuf;
  control_fd = socket(hisctladdr.sin_family,SOCK_STREAM,0);
  if (control_fd < 0)
  {
    iprintf("Ftp::test socket\n");
    code = -1;
    close(control_fd);
    control_fd = -1;
    return (NULL);
  }

  iSockFlags = fcntl(control_fd,F_GETFL,0);
  if (iSockFlags == -1) {
    iprintf("Ftp::test : F_GETFL on %d Failed,errno %d\n",control_fd,errno);
    return (NULL);
  }
  int flags = fcntl(control_fd,0);
  if (flags ==-1) {
          iprintf("Ftp::test unable to get flags,errno);
          return(NULL);
  }
  iprintf("current flags 0x%x\n",flags);
  if (flags & O_NONBLOCK)
          iprintf("O_NONBLOCK already set 0x%x\n",flags);
  else {
          iprintf("set O_NONBLOCK 0x%x\n",O_NONBLOCK);
          flags |= O_NONBLOCK;
          iprintf("flags 0x%x\n",flags);
          if(fcntl(control_fd,F_SETFL,flags)<0) {
                  iprintf("Error,Could not set O_NONBLOCK,errno);
                  return(NULL);
          }
  }
  return (NULL);
}

void main(void){
  test(host,port);
}

这似乎在我的 PC 上按预期工作,但在嵌入式目标平台上的工作方式似乎不同。在目标上,我看到以下消息:Ftp::test : F_GETFL on 15 Failed,errno 88 和 errno 88 是“套接字上的套接字操作”。 我想知道为什么会这样? 我们正在运行以下内核:

# uname -s    
Linux
# uname -r
4.1.18-gbbe8cfc

ARMv7 Processor rev 2 (v7l) cpu

根据下面@Shawn 的评论,我使用的 socket.h 标头 (fns_socket.h) 仅包含以下套接字类型:

#define SOCK_STREAM     1       /* virtual circuit */
#define SOCK_DGRAM      2       /* datagram */
#define SOCK_RAW        3       /* raw socket */
#define SOCK_RDM        4       /* reliable-delivered message */
#define SOCK_SEQPACKET  5       /* sequenced packet */
#define SOCK_PACK_EX    6       /* packet exchange non Berkeley */

即很遗憾,在创建时设置 SOCK_NONBLOCK 标志对我不起作用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。