c - 为什么 CTRL-D 没有用这个程序发出 EOF 信号?

标签 c sockets unix stdin eof

<分区>

当我运行这个程序时,我输入了一个 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;
}

最佳答案

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

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

另见 Canonical vs non-canonical terminal input .

您还应该始终检查 fgets() 和任何其他输入函数的返回值。如果输入函数报错,说明你用不起输入;它的状态是未定义的。

关于c - 为什么 CTRL-D 没有用这个程序发出 EOF 信号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20340561/

相关文章:

c - 返回指向c中指针的指针

c - Tiny C Compiler 生成的代码发出额外的(不必要的?)NOP 和 JMP

c# - 从两个不同的 IP 连接到服务器不起作用(聊天应用程序)

xml - XSLT用于具有相同名称的多个元素

c - 为什么点在释放时会崩溃?

c - 打印时我的 float 有多余的数字

C++ 与 asio 的双工套接字通信

java - 除非连接在超时前关闭,否则 Android 套接字读取不起作用

Mysql:选择今天起的所有订单(转换unix时间戳)

linux - glibc-2.14 当 'make install' 失败并出现 2 个错误