我试图创建一个程序来解释从客户端发送到服务器的命令。我的问题是我只能阅读命令的第一个单词。
字典.h
typedef struct
{
pid_t pid_cliente;
int status;
char command[TAM_MAX];
char secure[TAM_MAX];
char password[TAM_MAX];
int client_type;
} request;
客户端
printf("[ADMIN]: ");
scanf("%s[^\n]", buffer); //reads command
printf("Sending -> '%s'\n", buffer);
strcpy(request.command, buffer, MAX_SIZE-1) //MAX_SIZE = 50
write(server_fifo, & request, sizeof(request));
服务器端
read_res = read(server_fifo, & request, sizeof(request));
if (read_res < sizeof(request))
{
if (!strncasecmp("exit",(char *) & request,4)) return;
else
{
fprintf(stderr, "\nMessage Error!");
return;
}
}
token = strtok(request.command, " "); //reads command
printf("Command -> '%s'\n", token)
..
interprets wich command to do
..
token = strtok(NULL, " "); //reads command argument
printf("Argument -> '%s'\n", token);
输入:
addcity lisbon
输出:
Sending -> addcity
Sending -> lisbon
Command -> addcity
Argument -> (NULL)