簡體   English   中英

C程序不讀取鍵盤輸入

[英]C program not reading keyboard input

編輯:問題是 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
``

發生這種情況是因為緩沖區不為空並且此函數從中讀取。 要清空緩沖區,請嘗試以下代碼:

while( getchar() != '\n');

它將清空緩沖區,然后函數將等待輸入。

printf( "Enter a value :");
   c = getchar( );

或者

#include <stdio.h>
int main( ) {

   char str[100];
   int i;

   printf( "Enter a value :");
   scanf("%s %d", str, &i);

   printf( "\nYou entered: %s %d ", str, i);

   return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM