【问题标题】:Why isn't CTRL-D signaling EOF with this program? [duplicate]为什么 CTRL-D 不向这个程序发出 EOF 信号? [复制]
【发布时间】:2013-12-03 00:24:18
【问题描述】:

当我运行这个程序时,我输入一个 IP 地址和服务器端口作为输入。但是,在此之后程序要求我“请输入味精:”。但是,在输入我的消息后,按 ctrl-D 不会执行任何操作。程序仍然从 STDIN 读取。如何指示我正在输入的消息的结尾?

这是我正在使用的程序:

/* 
 * echoclient.c - A simple connection-based client
 * usage: echoclient <host> <port>
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

#define BUFSIZE 1024

/* 
 * error - wrapper for perror
 */
void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char **argv) {
    int sockfd, portno, n;
    struct sockaddr_in serveraddr;
    struct hostent *server;
    char *hostname;
    char buf[BUFSIZE];

    /* check command line arguments */
    if (argc != 3) {
       fprintf(stderr,"usage: %s <hostname> <port>\n", argv[0]);
       exit(0);
    }
    hostname = argv[1];
    portno = atoi(argv[2]);

    /* socket: create the socket */
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");

    /* gethostbyname: get the server's DNS entry */
    server = gethostbyname(hostname);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host as %s\n", hostname);
        exit(0);
    }

    /* build the server's Internet address */
    bzero((char *) &serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
      (char *)&serveraddr.sin_addr.s_addr, server->h_length);
    serveraddr.sin_port = htons(portno);

    /* connect: create a connection with the server */
    if (connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) 
      error("ERROR connecting");

    /* get message line from the user */
    printf("Please enter msg: ");
    bzero(buf, BUFSIZE);
    fgets(buf, BUFSIZE, stdin);


    /* write: send the message line to the server */
    n = write(sockfd, buf, strlen(buf));
    if (n < 0) 
      error("ERROR writing to socket");

    /* read: print the server's reply */
    bzero(buf, BUFSIZE);
    n = read(sockfd, buf, BUFSIZE);
    if (n < 0) 
      error("ERROR reading from socket");
    printf("Echo from server: %s", buf);
    close(sockfd);
    return 0;
}

【问题讨论】:

  • 看起来程序应该停止读取stdin,只要您在消息的第一行末尾点击“enter”。是什么让您认为它仍在读取输入?
  • 如果我按下回车键,它只会转到下一行并继续接受更多输入。
  • 我怀疑该程序已经转到n = read(sockfd, buf, BUFSIZE) 并正在等待从服务器获得响应,同时没有收听来自键盘的输入。此时您可以在键盘上键入任何您想要的内容,包括 Ctrl-D,但程序不会注意到它,直到 read 调用返回。
  • 如果我想要来自服务器的响应,我是在客户端之前运行服务器程序还是在服务器之前运行客户端?
  • 如果你在启动服务器之前运行客户端程序,那么客户端将没有任何东西可以连接:-)

标签: c sockets unix stdin eof


【解决方案1】:

如果您在该行输入了任何数据,第一个 Control-D 会将您输入的内容发送到终端,尽管您还没有点击返回。然后您需要再次键入 Control-D 以发送 0 个字符,这表示 EOF 到标准 I/O 库。

如果您输入的最后一个字符是回车,则保存的数据无论如何都会发送到终端。如果下一个字符是 Control-D,则不发送任何数据,因此读取内容为 0 字节,并被检测为 EOF。

另见Canonical vs non-canonical terminal input。

您还应该并且始终检查来自fgets() 和任何其他输入函数的返回值。如果输入功能报告失败,则无法使用输入;它的状态是未定义的。

【讨论】:

    【解决方案2】:

    需要测试fgets(buf, BUFSIZE, stdin);的结果

    在不测试结果的情况下,buf 可能包含其未更改的先前数据,并且不提供没有更多数据被读取的确定信息。

    【讨论】:

    • 如何测试结果?
    • @user2264035 if ( result = fgets(…) ) ….
    • @user2264035 if (fgets(buf, BUFSIZE, stdin) == NULL) Handle_EOF_or_IOError();
    【解决方案3】:

    fgets 一直读取直到到达 EOF 或换行符,就像为每个字节调用 fgetc 一样。在终端上按 CtrlD 不会在终端上“设置 EOF 状态”(没有这样的事情)或“发送和 EOF”(也没有这样的事情) .它所做的(在“cooked 模式”下)是使内核通过 tty 设备发送为行编辑缓冲的所有内容,以便应用程序接收它。如果还没有任何输入,这将导致应用程序以零长度 read 成功读取;这是EOF的定义,将设置stdioFILE流的EOF状态。

    但是,如果已经有输入等待(您输入的行),CtrlD 只会将其发送到应用程序。然后fgets 将读取所有传输的字节,并且看不到 EOF 或换行符,它将像再次调用fgetc 一样,再次阻塞read。如果你再按CtrlD或按Enter,fgets就会成功并返回。

    【讨论】:

      猜你喜欢
      • 2019-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      • 2012-10-09
      • 2020-08-14
      • 2019-04-30
      • 2023-01-20
      相关资源
      最近更新 更多