Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.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++ 为什么C++;在我按下enter键之前,输出不会到达插座?_C++_Linux_Sockets - Fatal编程技术网

C++ 为什么C++;在我按下enter键之前,输出不会到达插座?

C++ 为什么C++;在我按下enter键之前,输出不会到达插座?,c++,linux,sockets,C++,Linux,Sockets,我试图实现一个shell仿真器,作为必须使用套接字作为传输介质的任务的一部分。我目前面临的问题是,在调用list命令以列出活动进程(在服务器上调用)时,客户端直到按下Enter键后才接收数据。附加相关的代码片段: server.cpp else if(strcmp(token, "list") == 0) { token = strtok(NULL, " "); if (tok

我试图实现一个shell仿真器,作为必须使用套接字作为传输介质的任务的一部分。我目前面临的问题是,在调用list命令以列出活动进程(在服务器上调用)时,客户端直到按下Enter键后才接收数据。附加相关的代码片段:

server.cpp

else if(strcmp(token, "list") == 0) {
                    token = strtok(NULL, " ");
                    if (token == NULL) { //Active Processes
                        //char headerBuffer[256];
                        //int n = sprintf(headerBuffer, "%d", i);
                        //write(STDOUT_FILENO, headerBuffer, n);
                        //write(msgsock, headerBuffer, n);
                        char output[4096];
                        char temp[256];

                        for(int it = 0; it < i; it++) {
                            if (processes[it].status == 1) {
                                processes[it].elapsedTime = difftime(time(0), processes[it].startTime);
                                sprintf(temp, "Process Name: %s\n", processes[it].processName);
                                strcat(output, temp);
                                bzero(temp, sizeof(temp));

                                sprintf(temp, "Process ID: %d\n", processes[it].processID);
                                strcat(output, temp);
                                bzero(temp, sizeof(temp));

                                sprintf(temp, "Process Status [0 - Closed, 1 - Running]: %d\n", processes[it].status);
                                strcat(output, temp);
                                bzero(temp, sizeof(temp));  

                                sprintf(temp, "Starting Time: %s", ctime(&processes[it].startTime));
                                strcat(output, temp);
                                bzero(temp, sizeof(temp));

                                sprintf(temp, "Elapsed Time: %ds\n", (int) processes[it].elapsedTime);
                                strcat(output, temp);
                                bzero(temp, sizeof(temp));
                            }
                        }
                        if (write(msgsock, output, strlen(output)) == -1) {
                            perror("write to socket");
                        }
                        write(STDOUT_FILENO, output, strlen(output));
                        bzero(output, sizeof(output));
while (true) {
    int bytes_read = read(STDIN_FILENO, buf, 1024);
    write(sock_child, buf, bytes_read);

    int to_write = read(sock_child, output, 4096);
    write(STDOUT_FILENO, output, to_write);
    bzero(output, sizeof(output));
    //write(STDOUT_FILENO, "\n", 1);
}

如果你不知道问题是什么,你如何确定什么是相关的?在这种情况下,限制因素可能是您首先获取用户数据的方式。所有的标准C和C++输入例程都不会把用户输入从缓冲区移到你输入的任何输入,直到输入。我已经实现了其他功能,比如从客户端到服务器端的加/减数字,然后将结果返回给客户端。我不需要按enter键就可以显示输出,只要我发出命令并按enter键,输出就会返回。您的客户端只需要等待双方中的一方发送数据。它如何知道哪一个是正确的等待?