mserver_cmd.c

来自「linux下的人机对话编程」· C语言 代码 · 共 143 行

C
143
字号
#include<sys/types.h>#include<sys/stat.h>#include<stdio.h>#include<stdlib.h>#include<fcntl.h>#include<errno.h>#include<unistd.h>#include<syslog.h>#include<string.h>#include"socket.h"#include"print.h"#include"get_state.h"#include"write_log.h"#include"liansuo_count.h"#define STATE_FILE "/home/alpha/cfl/state.log"main(){        int i,fo;        char cmd[80],cmd2[80];        char buffer[520]={};//get the state info        get_state(STATE_FILE);//check M        if(ap->Mstate_gl==0)        {                printf("M_server error!\n");                exit(EXIT_FAILURE);        }        print(); while((i=getchar())!='0') {  switch(i)  {                                                                                                     case '1':        {                        int M;                        char A_res[10]={},B_res[10]={},M_res[10]={};                        printf("\ninput the command:");                        scanf("%s%s",cmd,cmd2);			strcat(cmd," ");                        strcat(cmd,cmd2);        if((ap->Astate_gl==1)&&(ap->Bstate_gl==1))                {                        socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,cmd);                        strcpy(A_res,check_error);		        socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,cmd);                        strcpy(B_res,check_error);		//compare the result now                if(strcmp(A_res,B_res)==0)                        {                        if(socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,"Accept the result!\n")==-1)                                {                                socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,"Accept the result!\n");                                }			 printf("A_res:%s is accepted\n",A_res);                        }                else                        {                        M=liansuo_function();                        sprintf(M_res,"%d",M);                        if(strcmp(A_res,M_res)==0)                                {                                socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,"Accept the result!\n");				printf("A_res:%s is accepted\n",A_res);                                }			 else if(strcmp(B_res,M_res)==0)                                {                                printf("B_res:%s is accepted\n",B_res);                                socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,"Accept the result!\n");                                }                                                                                                                            }                }        else if((ap->Astate_gl==0)&&(ap->Bstate_gl==1))                {                        socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,cmd);                        strcpy(B_res,check_error);                        M=liansuo_function();                        sprintf(M_res,"%d",M);                        if(strcmp(B_res,M_res)==0)                                {                                printf("B_res:%s is accepted\n",B_res);                                socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,"Accept the result!\n");                                }                        else                                printf("重发命令\n");                }         else if((ap->Astate_gl==1)&&(ap->Bstate_gl==0))                {                        socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,cmd);                        strcpy(A_res,check_error);			M=liansuo_function();                        sprintf(M_res,"%d",M);                        if(strcmp(A_res,M_res)==0)                                {                                printf("A_res:%s is accepted\n",A_res);                                socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,"Accept the result!\n");                                }                        else                                printf("重发命令\n");                }        else printf("两台都未启动\n");	print();	break;	}//case                                                                                                    	case '2':	{			printf("\ninput the process name for call:");                        scanf("%s%s",cmd,cmd2);                        strcat(cmd," ");                        strcat(cmd,cmd2);	if((ap->Astate_gl==1)&&(ap->Bstate_gl==1))		{		printf("与A B机通信成功!\n");		socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,cmd);		printf("%s\n",check_error);		socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,cmd);                printf("%s\n",check_error);		}	else if((ap->Astate_gl==1)&&(ap->Bstate_gl==0))		{		printf("与A机通信成功,B机需人工启动!\n");		socket_com(ap->Ahost_name_gl,ap->Amain_port_gl,cmd);                printf("%s\n",check_error);		}	else if((ap->Astate_gl==0)&&(ap->Bstate_gl==1))                {                printf("与B机通信成功,A机需人工启动!\n");                socket_com(ap->Bhost_name_gl,ap->Bmain_port_gl,cmd);                printf("%s\n",check_error);                }	else  printf("与A B机通信都不成功,都需人工启动!\n");	print();	break;	}//case        case '0':break;     }   } }//main

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?