C程序未读取键盘输入

问题描述 投票:0回答:1

编辑:问题是scanf(以及我在这里尝试的其他功能)没有等待输入,程序也没有暂停。

在Mac上的Virtual Box上使用Ubuntu 18我正在使用POSIX编写服务器/客户端。我无法在client.c中读取键盘输入。

 char action_type[1];
    printf("Chose action: T to request time, S to shut down\n");
    scanf(" %c",action_type);
    printf("%s", action_type);


如果我在main.c中输入与第一件事相同的代码,它将正常工作。

服务器/客户端和公用程序的完整代码是:

服务器

#include <stdio.h>
#include "mqueue.h"
#include "commons.h"
#include "errno.h"
#include "stdlib.h"
#include <string.h>
#include <time.h>


int status_closing = 0;

void send_time(char* clients_pid);
void send_activation(char* clients_pid);


//VARIABLES related to SERVERS QUEUE

mqd_t server; // server


struct mq_attr servers_attributes; // server creation attributes

struct mq_attr receiving_attributes; // server receiving attributes

// set up attributes

void set_servers_attributes(){
    // set up server's attributes
    servers_attributes.mq_maxmsg = QUEUE_SIZE;
    servers_attributes.mq_msgsize = MESSAGE_SIZE;
    printf("attributes set \n");
};

// open server

void open_servers_queue() {

    server = mq_open (servers_path,
                      O_CREAT | O_RDWR | O_EXCL ,
                      0666, &servers_attributes);


    if (server == -1) {
        printf("failed to open server's queue\n");
        printf(errno);
        exit(-1);
    } else {
        printf("opened servers queue as: %d\n",server);


    }
};

// check attributes

void check_attributes(){

    if ((mq_getattr(server,&receiving_attributes)) == -1) {

        printf("cannot read server's queue\n exit \n");
        exit(-1);
    }
};

void close_and_unlink_queue(){

    printf("At exit closing and unlinking queue\n");

    mq_close(server);
    mq_unlink(servers_path);

};

int check_for_messages_in_the_queue(){

    //printf("checking for messages\n");

    int messages_in_queue;
    messages_in_queue = receiving_attributes.mq_curmsgs;
    //printf("there are %d message in the servers queue\n",messages_in_queue);
   // printf("message in the queue!\n");
    return messages_in_queue;


};

char* receive_message(){

    char *receiving_buffer = malloc(sizeof(char)*MESSAGE_SIZE);

    if ((mq_receive(server,receiving_buffer,MESSAGE_SIZE,NULL))>0){

        return receiving_buffer;
    }

    else {

        printf("Server failed to receive message");

        mq_close(server);
        mq_unlink(servers_path);

        exit(-1);


    }

};


void respond(char *message_type, char* clients_pid) {

    printf("responding\n");

    char* type_one = "1";
    char* type_two = "2";
    char *type_three ="3";

    if (strcmp(message_type, type_one) == 0) {

        printf("TYPE 1\n");

        send_activation(clients_pid);

    }

    if (strcmp(message_type, type_two) == 0) {

        printf("TYPE 2\n");

       send_time(clients_pid);


    }

    if (strcmp(message_type, type_three) == 0) {

        printf("type 3 - SHUTDOWN INITIATED\n");

    }

}

void send_time(char* clients_pid) {

    time_t mytime = time(NULL);
    char *time_str = ctime(&mytime);
    time_str[strlen(time_str)-1] = '\0';





    char *clientpath[20];
    int clients_pid_int = atoi(clients_pid);
    sprintf(clientpath,"/%d",clients_pid_int);
    printf("clients path: %s\n",clientpath);


    mqd_t  client;
    client = mq_open(clientpath,O_RDWR , 0666, &servers_attributes);

    if (client == -1) {
        printf("failes opening client's queue \n");
        exit(-1);
    } else {
        printf("connected to client's queue: %d\n",client);
    }



};


void send_activation(char* clients_pid) {

    char *clientpath[20];
    int clients_pid_int = atoi(clients_pid);
    sprintf(clientpath,"/%d",clients_pid_int);
    printf("clients path: %s\n",clientpath);


    mqd_t  client;
    client = mq_open(clientpath,O_RDWR , 0666, &servers_attributes);

    if (client == -1) {
        printf("failes opening client's queue \n");
        exit(-1);
    } else {
        printf("connected to client's queue: %d\n",client);
        char* activation = malloc(sizeof(char)*MESSAGE_SIZE);
        char* activation_literal = "activation";
        sprintf(activation,"%s",activation_literal);

        int message_sent = mq_send(client,activation,MESSAGE_SIZE,0);
        printf("message sent with: %d",message_sent);





    }
};


int main() {

    // clean remainings of previous trials

    mq_close(server);
    mq_unlink(servers_path);

    // define atexit behaviour

    atexit(close_and_unlink_queue);

    //set servers attributes:

    set_servers_attributes();

    // open server's queue

    open_servers_queue();

    // receiving messages in the loop

    int condition = 1;

    while (1) {

        check_attributes();

        if (check_for_messages_in_the_queue() > 0) {

            char *received_message = receive_message();

            char *tok_one = strtok(received_message," ");
            char *tok_two = strtok(NULL, " ");
            //int clients_pid = atoi(tok_two);

            respond(tok_one,tok_two);

        } else if ((check_for_messages_in_the_queue() ==0) && (status_closing == 1)) {

            printf("Server's queue is empty - work finished. closing down\n");
            exit(0);


        }

    }
        printf("Hello, World!\n");
        return 0;

}

客户

#include <stdio.h>
#include <mqueue.h>
#include <unistd.h>
#include "commons.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>

mqd_t client;
mqd_t server;
char clientpath[20];
char* sending_buffer[MESSAGE_SIZE];

struct mq_attr clients_attributes;

struct mq_attr receiving_attributes;

void connect_to_server(){

    server = mq_open(servers_path,O_WRONLY);
    if(server == -1) {
        printf("connection to server failed\n");

    } else {

        printf("connected to server with id: %d \n",server);
    }
};

void set_clients_attributes(){


    // deal with attributes

    clients_attributes.mq_maxmsg = QUEUE_SIZE;
    clients_attributes.mq_msgsize = MESSAGE_SIZE;

};







void create_clients_queue(){


    // create clients path
    pid_t client_pid = getpid();

    sprintf(clientpath, "/%d", client_pid);


    // open clients queue
    client = mq_open(clientpath,O_RDONLY | O_CREAT | O_EXCL, 0666, &clients_attributes);
   // printf(errno);

    if (client == -1) {
        printf("failes opening client's queue \n");
        exit(-1);
    } else {
        printf("connected to client's queue: %d\n",client);
    }
};

void register_at_server(){

    message message;
    message.mtype = 1;
    int client_pid = getpid();
    message.sender = client_pid;
    char separator = ' ';


    snprintf(sending_buffer,MESSAGE_SIZE,"%ld%c%d",message.mtype,separator,message.sender);


    if ((mq_send(server,sending_buffer, MESSAGE_SIZE,0)) == -1) {
        printf("failed to send registration request\n");
        exit(-1);
    }

    else {

        printf("%s",sending_buffer);
        printf("sent registration request\n");


    }

};

void close_and_unlink_queue(){

    printf("At exit closing and unlinking queue\n");

    mq_close(client);
    mq_unlink(clientpath);

};


void check_attributes(){

    if ((mq_getattr(client,&receiving_attributes)) == -1) {

        printf("cannot read own's queue\n exit \n");
        exit(-1);
    }
};

int check_for_messages_in_the_queue(){

    //printf("checking for messages\n");

    int messages_in_queue;
    messages_in_queue = receiving_attributes.mq_curmsgs;
    //printf("there are %d message in the servers queue\n",messages_in_queue);
    // printf("message in the queue!\n");
    return messages_in_queue;


};

char* receive_message(){

    char *receiving_buffer = malloc(sizeof(char)*MESSAGE_SIZE);

    if ((mq_receive(client,receiving_buffer,MESSAGE_SIZE,NULL))>0){

        return receiving_buffer;
    }

    else {

        printf("Server failed to receive message");

        mq_close(client);
        mq_unlink(clientpath);

        exit(-1);


    }

};



void choose_action(){

    char action_type[1];
    printf("Chose action: T to request time, S to shut down\n");
    scanf(" %c",action_type);
    printf("%s", action_type);











};





int main() {

    mq_close(client);
    mq_unlink(clientpath);

    connect_to_server();

    set_clients_attributes();

    create_clients_queue();

    register_at_server();

    int condition = 1;
    int client_active = 0;

    while (condition) {

        check_attributes();

        if (check_for_messages_in_the_queue() > 0) {
            char *received_message = receive_message();
            if ((strcmp(received_message, "activation")) == 0) {
                client_active = 1;
                condition = 0;
                free(received_message);
            }
        }
    }

    char action_type[1];
    printf("Chose action: T to request time, S to shut down\n");
    scanf(" %c",action_type);
    printf("%s", action_type);









    printf("Hello, World!\n");
        return 0;

}

普通

#ifndef SERVER_COMMONS_H
#define SERVER_COMMONS_H

#include <signal.h>

// define values of server queue attributes

//define message struct

typedef struct messgae {
    char content[4096];
    pid_t sender;
    long mtype;

} message;



#define QUEUE_SIZE 10
#define MESSAGE_SIZE sizeof(message)

// define servers path
const char servers_path[] = "/server";






#endif //SERVER_COMMONS_H
``

编辑:问题是scanf(以及我在这里尝试的其他功能)没有等待输入,程序也没有暂停。在Mac上的Virtual Box上使用Ubuntu 18,我正在使用...

c scanf posix
1个回答
0
投票

发生这种情况是因为缓冲区不为空,并且该函数从其中读取了该缓冲区。要清空缓冲区,只需尝试以下代码:

© www.soinside.com 2019 - 2024. All rights reserved.