【问题标题】:select() working for STDIN but not socketsselect() 适用于 STDIN 但不适用于套接字
【发布时间】:2020-08-26 02:07:03
【问题描述】:

我有以下用于 TCP 客户端的 C 代码:

#define PORT 8888


void connexion(int sock)
{
    fd_set rfds;
    struct timeval tv;
    int retval;

    FD_ZERO(&rfds);
    FD_SET(0, &rfds);
    FD_SET(sock, &rfds);
    char buf[512];

    retval = select(3, &rfds, NULL, NULL, NULL);

    if (retval == -1)
        perror("select()");
    else if (retval) {
        read(retval, buf, 512);
        printf("\nbuf: %s", buf);
        memset(buf, 0, 512);
    }
    connexion(sock);
}

int main(int argc, char const *argv[])
{
    int sock = 0, valread;
    struct sockaddr_in serv_addr;
    char *hello = "Hello from client";
    char buffer[1024] = {0};
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        printf("\n Socket creation error \n");
        return -1;
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
    {
        printf("\nInvalid address/ Address not supported \n");
        return -1;
    }

    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    {
        printf("\nConnection Failed \n");
        return -1;
    }
    connexion(sock);
}

我尝试在不停止读取套接字的情况下读取标准输入。确实,我需要能够在写消息的同时接收消息。为此,我使用了选择功能。但我只从终端接收消息,从不接收来自服务器的消息。

服务器仍然收到来自客户端的套接字...这段代码有什么问题?

提前谢谢你。

【问题讨论】:

  • in select(3, &amp;rfds, NULL, NULL, NULL); 3 必须替换为sock+1
  • read(retval, buf, 512);这里的retval可能是0或1,你想要retval = read(soc, buf, 512);

标签: c sockets select networking tcp


【解决方案1】:

你没有正确使用select,替换

    retval = select(3, &rfds, NULL, NULL, NULL);

通过

    retval = select(sock+1, &rfds, NULL, NULL, NULL);

select() 为 STDIN 工作 ...

适用于 stdin 因为 stdin 是 1 所以小于 3

在里面

read(retval, buf, 512);

不做你期望的,做

if (FD_ISSET(sock, &rfds)) {
  read(sock, buf, 512);
  printf("buf from socket: %s\n", buf);
}
if (FD_ISSET(stdin, &rfds)) {
  read(stdin, buf, 512);
  printf("buf from stdin: %s\n", buf);
}

警告如果您在套接字上读取并且未发送/读取空字符,因为printf 将具有未定义的行为,可能会保存读取长度以在末尾添加空字符

如果套接字已关闭或您在 stdin 上到达 EOF,则 read 可能什么都没有读取,因此最好检查它们的结果

不要在两个if 之间使用else,因为您可以同时获得两者的输入

你的终端递归可以用循环代替

在您的printf 中,在刷新输出之前而不是之后生成换行符很奇怪

char *hello = "Hello from client";
char buffer[1024] = {0};

未使用

【讨论】:

  • 谢谢,但是当我输入:c read(sock, buf, 512); 时,我只能从套接字读取消息。现在,我无法得到提示。如何从套接字接收消息并获取提示内容?我已经尝试过 ISSET 但无法正常工作...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 1970-01-01
  • 2023-02-26
相关资源
最近更新 更多