C程序不读取键盘输入

C程序不读取键盘输入,c,scanf,posix,C,Scanf,Posix,编辑:问题是scanf(以及我在这里尝试的其他函数)没有等待输入,程序没有暂停 在Mac虚拟机上使用Ubuntu18 我正在使用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);

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

在Mac虚拟机上使用Ubuntu18 我正在使用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;

}
#包括
#包括“mqueue.h”
#包括“commons.h”
#包括“errno.h”
#包括“stdlib.h”
#包括
#包括
int status_closing=0;
无效发送时间(字符*客户机pid);
无效发送激活(字符*客户端\u pid);
//与服务器队列相关的变量
mqd_t server;//服务器
struct mq_attr servers_attributes;//服务器创建属性
结构mq_attr接收_属性;//服务器接收属性
//设置属性
无效集合\u服务器\u属性(){
//设置服务器的属性
servers\u attributes.mq\u maxmsg=队列大小;
servers\u attributes.mq\u msgsize=消息大小;
printf(“属性集\n”);
};
//开放服务器
无效打开的\u服务器\u队列(){
服务器=mq\u打开(服务器路径,
O|u创建| O|RDWR | O|u不包括,
0666和服务器(U属性);
如果(服务器==-1){
printf(“打开服务器队列失败\n”);
printf(errno);
出口(-1);
}否则{
printf(“打开的服务器队列为:%d\n”,服务器);
}
};
//检查属性
无效检查_属性(){
if((mq_getattr(服务器和接收_属性))=-1){
printf(“无法读取服务器的队列\n退出\n”);
出口(-1);
}
};
无效关闭和取消链接队列(){
printf(“在出口关闭和断开队列链接时\n”);
mq_关闭(服务器);
mq_取消链接(服务器_路径);
};
检查队列()中的消息{
//printf(“检查消息\n”);
int消息在队列中;
消息\u在\u队列中=接收\u attributes.mq\u curmsgs;
//printf(“服务器队列中有%d条消息,\n”,消息队列中有\u);
//printf(“队列中的消息!\n”);
返回队列中的消息;
};
字符*接收消息(){
char*接收缓冲区=malloc(sizeof(char)*消息大小);
if((mq_接收(服务器、接收_缓冲区、消息_大小、NULL))>0){
返回接收缓冲区;
}
否则{
printf(“服务器无法接收消息”);
mq_关闭(服务器);
mq_取消链接(服务器_路径);
出口(-1);
}
};
无效响应(字符*消息类型,字符*客户端\u pid){
printf(“响应的\n”);
字符*type_one=“1”;
char*type_two=“2”;
字符*type_three=“3”;
if(strcmp(消息类型,类型一)==0){
printf(“类型1\n”);
发送\u激活(客户端\u pid);
}
if(strcmp(消息类型,类型二)==0){
printf(“类型2\n”);
发送时间(客户机pid);
}
if(strcmp(消息类型,类型三)==0){
printf(“类型3-启动关机\n”);
}
}
无效发送时间(字符*客户端\u pid){
time\u t mytime=时间(空);
char*time\u str=ctime(&mytime);
时间序列[strlen(time-str)-1]='\0';
char*clientpath[20];
int clients\u pid\u int=atoi(clients\u pid);
sprintf(客户端路径,“/%d”,客户端\u pid\u int);
printf(“客户端路径:%s\n”,clientpath);
mqd_t客户端;
client=mq_open(clientpath、O_RDWR、0666和servers_属性);
如果(客户端==-1){
printf(“打开客户端队列失败\n”);
出口(-1);
}否则{
printf(“已连接到客户端队列:%d\n”,客户端);
}
};
无效发送激活(字符*客户端\u pid){
char*clientpath[20];
int clients\u pid\u int=atoi(clients\u pid);
sprintf(客户端路径,“/%d”,客户端\u pid\u int);
printf(“客户端路径:%s\n”,clientpath);
mqd_t客户端;
client=mq_open(clientpath、O_RDWR、0666和servers_属性);
如果(客户端==-1){
printf(“打开客户端队列失败\n”);
出口(-1);
}否则{
printf(“已连接到客户端队列:%d\n”,客户端);
char*激活=malloc(sizeof(char)*消息大小);
char*activation_literal=“activation”;
sprintf(激活,“%s”,激活文字);
int message_sent=mq_send(客户端,激活,消息大小,0);
printf(“与%d一起发送的消息”,已发送的消息);
}
};
int main(){
//以前试验的干净剩余物
mq_关闭(服务器);
mq_取消链接(服务器_路径);
//定义退欧行为
atexit(关闭和取消链接队列);
//设置服务器属性:
设置服务器属性();
//打开服务器的队列
打开服务器队列();
//在循环中接收消息
int条件=1;
而(1){
检查_属性();
if(检查队列()中的\u消息\u>0){
char*received_message=received_message();
char*tok_one=strtok(收到消息“”);
char*tok_two=strtok(空,“”);
//int clients_pid=atoi(tok_two);
响应(tok_1,tok_2);
}else if((检查队列()中的消息)&&(状态关闭==1)){
printf(“服务器队列为空-工作已完成。正在关闭\n”);
出口(0);
}
}
printf(“你好,世界!\n”);
返回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;

}
#包括
#包括
#包括
#包括“commons.h”
#包括
#包括
#包括
mqd_t客户端;
mqd_t服务器;
char-clientpath[20];
字符*发送缓冲区[消息大小];
结构mq_attr clients_属性;
结构mq_attr接收_属性;
无效连接\u到\u服务器(){
server=mq\u open(服务器路径,仅限O\u);
如果(服务器==-1){
printf(“与服务器的连接失败\n”);
}否则{
printf(“已连接到id为%d\n的服务器”,服务器);
}
};
无效集\客户端\属性(){
//处理属性
clients\u attributes.mq\u maxmsg=队列大小;
clients_attributes.mq_msgsize=消息大小;
};
无效创建\u客户端\u队列(){
//创建客户端路径
pid_t client_pid=getpid();
sprintf(客户端路径“/%d”,客户端pid);
//
#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;
}