Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么write不允许我将数据写入连接的套接字_C_Sockets_Write_Ucos - Fatal编程技术网

C 为什么write不允许我将数据写入连接的套接字

C 为什么write不允许我将数据写入连接的套接字,c,sockets,write,ucos,C,Sockets,Write,Ucos,我的目标平台(在uCOS上运行)出现问题,write()似乎不允许我将数据写入已连接的客户端套接字(端口21),它返回一个负数,并将errno设置为11,这被解释为无更多进程-我不确定这意味着什么。 描述问题的示例函数如下所示: #include <stdio.h> #include <errno.h> #include <string.h> #include <unistd.h> #include <fcntl.h>

我的目标平台(在
uCOS
上运行)出现问题,
write()
似乎不允许我将数据写入已连接的客户端套接字(端口21),它返回一个负数,并将
errno
设置为
11
,这被解释为
无更多进程
-我不确定这意味着什么。 描述问题的示例函数如下所示:

  #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;
  static struct sockaddr_in myctladdr;
  int len; 

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

char *test(char *host, int port) {
  static char hostnamebuf[256];
  fd_set          L_rset;
  struct timeval  L_timeout;
  unsigned char   mychar;
  char            buf[10] ={'A','U','T','H',' ','T','L','S','\0','\n'};
  int             L_cmdLength;
  int             L_read;
  int         len;
  int         rv;
  int         i;

  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
  {
    printf("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)
  {
    printf("Ftp::test socket\n");
    code = -1;
    close(control_fd);
    control_fd = -1;
    return (NULL);
  }

  if (connect(control_fd, (struct sockaddr *)&hisctladdr, sizeof(hisctladdr)) < 0) {
    if (errno != EINPROGRESS)
    {
      printf("Ftp::test : connect failure\n");
    }
  }

  printf("Ftp::test : connected\n");

  len = sizeof(struct sockaddr_in);
  if (getsockname(control_fd, (struct sockaddr *)&myctladdr, &len) < 0)
  {
   printf("Ftp::test Error getsockname errno %d\n", errno);
    code = -1;
    close(control_fd);
    control_fd = -1;
    return (NULL);
  }
  L_timeout.tv_sec = 10;
  L_timeout.tv_usec = 0;
  // retrieve welcome message from server
  FD_ZERO(&L_rset);
  FD_SET(control_fd,&L_rset);  
  rv = select(control_fd + 1, &L_rset, NULL, NULL, &L_timeout);
  if (rv < 0){
    printf("Ftp::test error reading from socket %d\n", rv);
    return (NULL);
  } else if (rv >0) {
    for(i = 0; i < 2 &&FD_ISSET(control_fd, &L_rset);i++){
        L_read = recv(control_fd, (char *)&mychar, 1, 0);
        printf("received %c 0x%x\n",mychar,mychar);
    }
    printf("Ftp::test connection to server established!\n");
  } else {
    printf("No data received from server within 10 seconds, timeout!\n");
    return (NULL);
  }
  L_cmdLength = strlen(buf);
  printf("Ftp::test : write (%d, %s, %d)\n",control_fd, buf, L_cmdLength);
  rv = write(control_fd, buf, L_cmdLength);
  if (rv >= 0)
    printf("Ftp::test : sent %dBytes\n", rv);
  else 
    printf("Ftp:test : error writing data, errno %d %s\n",errno, strerror(errno));
  return (NULL);
}
void main(void){
  test(host,port);
}

为此,我将使用
send()
recv()
,而不是假设
read()
write()
在您的操作系统中也能工作。您是否读取了任何数据,或者是否有超时?注意:FTP中的行终止符定义为
\r\n
,而不仅仅是
\n
。您在[删除,我想]之前已经问过这个问题,从我和其他人那里得到了有效的回答。与前面一样,
errno
仅在给定函数返回错误(例如
-1
)时有效,否则为随机/垃圾。您的旧代码或此代码不可能生成“不再处理”错误[尤其是在
write
syscall上]。这只能来自代码中没有的
分叉。因此,我必须得出结论,您发布的输出消息不是来自发布的代码。在linux上,
errno
的值为11是
EAGAIN
(“重试”)@CraigEstey是的,我以前发布过,但后来有所改进<现在,仅当
write
的返回值为
是否交叉链接时,才会解释code>errno
?我想知道C库是否有正确的错误号错误文本。但是我怀疑
send()
会解决这个问题。但是实现Posix了吗?显然不是。它在Windows上也不工作。。。
Ftp::test : connected
received 2 0x32
received 2 0x32
Ftp::test connection to server established!
Ftp::test : write (14, AUTH TLS, 8)
Ftp:test : error writing data, errno 11 No more processes