zoukankan      html  css  js  c++  java
  • 基于MQTT协议的云端proxy远程登陆

    这篇文件是建立在一下两篇文章基础上完成的 很多重复的内容不会在这章提到
    https://www.cnblogs.com/y-c-y/p/11685405.html telnet协议相关
    https://www.cnblogs.com/y-c-y/p/11686916.html MQTT协议相关

    从这里开始就假设对MQTT和telnet协议的实现已经有一定的基础了

    这张时序图是整个程序的时序 数据流向也是按章箭头的流向来的

    一下为实现代码 部分的if 0是关掉了调试用信息

    其中的 telnet.c telnet.h utils.c utils.h都已经在之前的文章中贴出来了 这次主要是控制部分

    cloud_terninal.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <mosquitto.h>
    #include <string.h>
    
    #define HOST "localhost"
    #define PORT  1883
    #define KEEP_ALIVE 60
    #define MSG_MAX_SIZE  512
    #define TOPIC_NUM 3
    
    bool session = true;
    
    const static char* topic[TOPIC_NUM] =
    {
    	"Gai爷:",
    	"ycy ",
    	"CCYY "
    };
    
    void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
    {
    	
        if(message->payloadlen){
            printf(">%s %s", message->topic, (char *)message->payload);
    		//printf("--sdfs----%ld-----------------
    ",strlen((char *)message->payload));
    		
        }else{
            printf("%s (null)
    ", message->topic);
        }
        fflush(stdout);
    }
    
    void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
    {
        int i;
        if(!result){
            /* Subscribe to broker information topics on successful connect. */
            mosquitto_subscribe(mosq, NULL, "CCYY ", 2);
        }else{
            fprintf(stderr, "Connect failed
    ");
        }
    }
    
    void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
    {
        int i;
        printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
        for(i=1; i<qos_count; i++){
            printf(", %d", granted_qos[i]);
        }
        printf("
    ");
    }
    
    void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
    {
        /* Pring all log messages regardless of level. */
        printf("%s
    ", str);
    }
    
    int main()
    {
        struct mosquitto *mosq = NULL;
    	char buff[MSG_MAX_SIZE];
    	
        //libmosquitto 库初始化
        mosquitto_lib_init();
        //创建mosquitto客户端
        mosq = mosquitto_new(NULL,session,NULL);
        if(!mosq){
            printf("create client failed..
    ");
            mosquitto_lib_cleanup();
            return 1;
        }
        //设置回调函数,需要时可使用
        //mosquitto_log_callback_set(mosq, my_log_callback);
        mosquitto_connect_callback_set(mosq, my_connect_callback);
        mosquitto_message_callback_set(mosq, my_message_callback);
        mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
    	
    
        //连接服务器
        if(mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE)){
            fprintf(stderr, "Unable to connect.
    ");
            return 1;
        }
        //开启一个线程,在线程里不停的调用 mosquitto_loop() 来处理网络信息
        int loop = mosquitto_loop_start(mosq); 
        if(loop != MOSQ_ERR_SUCCESS)
        {
            printf("mosquitto loop error
    ");
            return 1;
        }
    
    
    	while(fgets(buff, MSG_MAX_SIZE, stdin) != NULL)
        {
            /*发布消息*/
            mosquitto_publish(mosq,NULL,"ycy ",strlen(buff)+1,buff,0,0);
            memset(buff,0,sizeof(buff));
        }
    
        mosquitto_destroy(mosq);
        mosquitto_lib_cleanup();
    
        return 0;
    }
    
    
    

    my_task.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <regex.h>
    #include"mosquitto.h"
    #include"utils.h"
    #include"telnet.h"
    
    
    //#define IP_ADDRESS   "127.0.0.1"
    #define IP_PORT      23
    #define SERV_PORT    3333
    #define MAXLINE      1024
    
    #define HOST "localhost"
    #define PORT  1883
    #define KEEP_ALIVE 60
    #define MSG_MAX_SIZE  512
    #define TOPIC_NUM 3
    #define Key_to_Fun_Size 3
    
    typedef struct sockaddr SA;
    typedef void (*Fun)() ;
    
    typedef struct key_word_to_fun
    {
    	char  key_word[MSG_MAX_SIZE];
    	Fun   fun;
        void* arg1;	
    }Key_to_Fun;
    
    
    static void log_in_telnet();
    
    
    bool session = true;
    uint32 SOCKFD = 0;
    char IP_ADDRESS[] = "xxx.xxx.xxx.xxx";
    
    const static char* topic[TOPIC_NUM] =
    {
    	"Gai爷:",
    	"ycy ",
    	"CCYY "
    };
    
    
    static Key_to_Fun tab_Key_to_Fun[Key_to_Fun_Size] =
    {
    	{"telnet",log_in_telnet,0},
    	{"login",log_in_telnet,0},
    	{"password",log_in_telnet,0},
    	//{NULL,NULL}
    };
    
    uint32 max(uint32 a, uint32 b)
    {
    	return (a>b?a:b);
    }
    
    void ERR_EXIT(char* s)
    {
    	perror(s);
    	exit(EXIT_FAILURE);
    }
    void INFO_PRINT(char* s)
    {
    	printf("%s",s);
    }
    
    uint32 match_string(char *src,char* dst,char *pattern)
    {
    	
        //char *pattern1 = "^telnet";
    	
    	char errbuf[MAXLINE];
    	//char match[MAXLINE];
    	regex_t reg;
    	int err,nm = 10;
    	regmatch_t pmatch[nm];
    
    	if(regcomp(&reg,pattern,REG_EXTENDED) < 0)
    	{
    		regerror(err,&reg,errbuf,sizeof(errbuf));
    		printf("err:%s
    ",errbuf);
    		return 0;
    	}
    	err = regexec(&reg,src,nm,pmatch,0);
    	if(err == REG_NOMATCH)
    	{
    		printf("no match
    ");
    		return 0;
    	}
    	else if(err)
    	{
    		regerror(err,&reg,errbuf,sizeof(errbuf));
    		printf("err:%s
    ",errbuf);
    		return 0;
    	}
    
    	for(int i=0;i<10 && pmatch[i].rm_so!=-1;i++)
    	{
    		int len = pmatch[i].rm_eo - pmatch[i].rm_so;
    		if(len)
    		{
    			memset(dst,'',MAXLINE);
    			memcpy(dst,src + pmatch[i].rm_so,len);
    			printf("%s
    ",dst);
    		}  
    	}
    	
    	return 1;
    
    }
    
    
    static void log_in_telnet()
    {
    	uint32 sockfd,isReady=0;
    	struct sockaddr_in servaddr;
    	uint32 hname[128];
    	
    	sockfd = socket(AF_INET,SOCK_STREAM,0);
    	bzero(&servaddr,sizeof(servaddr));
    	servaddr.sin_family = AF_INET;
    	servaddr.sin_port = htons(IP_PORT);
    	servaddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
    	printf("servaddr: IP is %s, Port is %d
    ",inet_ntoa(servaddr.sin_addr), ntohs(servaddr.sin_port));
    
    	while(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))){};
    	SOCKFD = sockfd;
    	printf("connect has been ready
    ");
    }
    
    static void search_key_word_to_fun(struct mosquitto *mosq,char *s)
    {
    	int i = 0,j = 0;
    	char temp[MSG_MAX_SIZE] = {};
    	char match[MAXLINE];
    	char pattern[] = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}";
    	
    	for(i =0 ;i< Key_to_Fun_Size;i++)
    	{
    		if(match_string(s,match,tab_Key_to_Fun[i].key_word))
    		{
    			if((i==0)&& (SOCKFD != 0))
    			{
    				printf("telnet has connect!! error command !!
    ");
    				return ;
    			}
    			else if((i==0)&& (SOCKFD == 0))
    			{
    			    if(match_string(s,match,pattern))
    				{
    					printf("---match------%s----------
    ",match);
    					for(j =0 ; match[j] != '' ;j++)
    					{
    						IP_ADDRESS[j] = match[j];
    					}
    					IP_ADDRESS[j] = '';
    					tab_Key_to_Fun[i].fun();
    				}
    				else
    				{
    					/* do nothing */
    					mosquitto_publish(mosq,NULL,"CCYY ",29,"you should put into your ip
    ",0,0);
    				}
    				
    			}
    			else
    			{
    				tab_Key_to_Fun[i].fun();
    			}
    			return ;
    		}
    	}
    	if((SOCKFD)&&(i != 0))
    	{
    		writen(SOCKFD,s,strlen(s));
    	}
    }
    
    static void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
    {
    	
        if(message->payloadlen){
            printf(">%s %s", message->topic, (char *)message->payload);
    		search_key_word_to_fun(mosq,(char *)message->payload);
    		
    		
    		//MQTT发啥 就往telnet发啥
        }else{
            printf("%s (null)
    ", message->topic);
        }
        //fflush(stdout);
    }
    
    static void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
    {
        int i;
        if(!result){
            /* Subscribe to broker information topics on successful connect. */
            mosquitto_subscribe(mosq, NULL, "ycy ", 2);
        }else{
            fprintf(stderr, "Connect failed
    ");
        }
    }
    
    static void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
    {
        int i;
        printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
        for(i=1; i<qos_count; i++){
            printf(", %d", granted_qos[i]);
        }
        printf("
    ");
    }
    
    static void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
    {
        /* Pring all log messages regardless of level. */
        printf("%s
    ", str);
    }
    /* 此处用这个函数代替之前的writen函数,是因为真正的telnet协议是一字符一字符发送的字符串结尾以'
    '结束 */
    void telnet_client_send_msg(int fd, const void *vptr, size_t n)
    {
    	char endFlag = '
    ';
    	uint32 i =0;
    	void * pStr = vptr;
    	
    	for(i = 0 ; i < n ; i++,pStr++ )
    	{
    		writen(fd,pStr,1);
    	}
    	if(*(uint8 *)pStr == '')
    	{
    		writen(fd,&endFlag,1);
    	}
    	else
    	{
    		writen(fd,&endFlag,1);
    	}
    }
    
    void wait_message_from_telnet(struct mosquitto *mosq,FILE *fp,uint32 sockfd)
    {
    	uint32 maxfdp1,nready;//stdineof;
    	fd_set rset;
    	uint8 buf[MAXLINE];
    	uint8 respbuff[MAXLINE] = {0};;
    	uint32 resplen;
    	uint32 n;
    	uint8 echo_cmd[] = {0xff,0xfb,0x01};
    	//stdineof = 0;
    	FD_ZERO(&rset);
    	writen(sockfd,echo_cmd,3);
    	
    	for(;;)
    	{
    		//if(stdineof == 0)
    		FD_SET(fileno(fp),&rset);
    		FD_SET(sockfd,&rset);
    		maxfdp1 = max(fileno(fp),sockfd)+1;
    		nready = select(maxfdp1,&rset,NULL,NULL,NULL);
    		
    		if(nready < 0)
    		{
    			ERR_EXIT("ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    		}
    		
    		if(FD_ISSET(sockfd,&rset))
    		{
    			memset(buf,0,MAXLINE);
    			if((n = read(sockfd,buf,MAXLINE))==0)
    			{
    				ERR_EXIT("str_cli:server termination prematurely");
    			}
    			buf[n] = '';
    			//printf("FD_ISSET(sockfd,&rset)------11------%s----22----------------
    ",buf);
    			
    			if(buf[0] == IAC)
    			{
    				memset(respbuff,0,MAXLINE);
    				resplen = get_every_frame(buf,n,respbuff,MAXLINE);
    				writen(sockfd,respbuff,resplen);
    			}
    			else
    			{
    				telnet_client_send_msg(fileno(stdout),(char *)buf,n);//当数据比较大的时候 会造成发给cloud数据错位
    				//printf("------------2------------
    ");
    				mosquitto_publish(mosq,NULL,"CCYY ",strlen(buf)+1,buf,0,0);
    				//printf("------------3------------
    ");
    				memset(buf,0,MAXLINE);
    			}
    			
    			//writen(fileno(stdout),buf,n);
    		}
    		if(FD_ISSET(fileno(fp),&rset))
    		{
    			memset(buf,0,MAXLINE);
    			if((n = readline(fileno(fp),(char *)buf,MAXLINE)) == 0)
    			{
    				//stdineof = 1;//此时碰到EOF 并且马上要发生FIN序列 所以标准输入不可读了
    				shutdown(sockfd,SHUT_WR);
    				FD_CLR(fileno(fp),&rset);
    				INFO_PRINT("nothing input!");
    				continue;
    			}
    			else if(n >0)
    			{
    				/* do nothing */
    			}
    			else
    			{
    				ERR_EXIT("some error occurred ");
    			}
    			//printf("FD_ISSET(fileno(fp),&rset)----%d--
    ",n);
    			//memset(buf,0,MAXLINE);
    			telnet_client_send_msg(sockfd,(char *)buf,n);
    			//send_message_to_cloud(sockfd,(char *)buf,n);
    		}
    	}
    }
    
    void test()
    {
    	//unsigned char s[] = 15042b58;
    	//printf("+++++++++++++++++++++++++++++++
    ",s);
    	printf("+++++++++++++++++++++++++++++++
    ");
    	putchar(0x15);
    	putchar(0x04);
    	putchar(0x2b);
    	putchar(0x58);
    	printf("+++++++++++++++++++++++++++++++
    ");
    }
    
    int main()
    {
        struct mosquitto *mosq = NULL;
    	char buff[MSG_MAX_SIZE];
    	
    	
    	test();
        //libmosquitto 库初始化
        mosquitto_lib_init();
        //创建mosquitto客户端
        mosq = mosquitto_new(NULL,session,NULL);
        if(!mosq){
            printf("create client failed..
    ");
            mosquitto_lib_cleanup();
            return 1;
        }
        //设置回调函数,需要时可使用
        //mosquitto_log_callback_set(mosq, my_log_callback);
        mosquitto_connect_callback_set(mosq, my_connect_callback);
        mosquitto_message_callback_set(mosq, my_message_callback);
        mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
    	
    
        //连接服务器
        if(mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE)){
            fprintf(stderr, "Unable to connect.
    ");
            return 1;
        }
        //开启一个线程,在线程里不停的调用 mosquitto_loop() 来处理网络信息
        int loop = mosquitto_loop_start(mosq); 
        if(loop != MOSQ_ERR_SUCCESS)
        {
            printf("mosquitto loop error
    ");
            return 1;
        }
    
    	while(!SOCKFD){};
    	wait_message_from_telnet(mosq,stdin,SOCKFD);
    	
    
        mosquitto_destroy(mosq);
        mosquitto_lib_cleanup();
    
        return 0;
    }
    
    
    

    makefile

    
    #不是系统默认库 要记得添加连接选项
    
    all:My_task
    	@echo ""
    	@echo "This is My_task compile......."
    	@echo ""
    	
    My_task: utils.o telnet.o my_task.o Cloud
    	gcc -g -o My_task my_task.o utils.o telnet.o -lmosquitto -lpthread
    
    Cloud: utils.o telnet.o cloud_terminal.o
    	gcc -g -o Cloud cloud_terminal.c -lmosquitto -lpthread
    	
    my_task.o:my_task.c utils.h telnet.h
    	gcc -g -c my_task.c
    	
    cloud_terminal.o:cloud_terminal.c utils.h telnet.h
    	gcc -g -c cloud_terminal.c	
    	
    utils.o:utils.c utils.h
    	gcc -g -c utils.c 
    	
    telnet.o:telnet.c telnet.h
    	gcc -g -c telnet.c
    	
    clean :
    	-rm my_task.o cloud_terminal.o utils.o telnet.o Cloud My_task
    
    

    运行情况:

    为了方便调试 在mytask的程序中我也打印出来登录信息

    =====================================================================================================

    以上的程序有一点不足之处是默认登录我自己的IP 不能从外界获取IP 一下的程序再这个基础上有所改动 可以使用“telnet 127.0.0.1”这种命令登录进去。

    添加的部分是使用了正则表达式,包含头文件#include <regex.h> 关于这个C语言的正则表达式的用法在此不详说 如果要匹配中文请慎重使用 据说对中文支持的不是很好

    我在这里遇见的问题是

    我想匹配“xxx.xxx.xxx.xxx”使用的可以识别模式串是char pattern[] = "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}";这个是没有问题的

    但是最开始我使用的是char pattern[] = "d{1,3}.d{1,3}.d{1,3}.d{1,3}"; 以上这两种用法在python中是相同的结果 但是第二种情况在C语言中不适用

    my_task.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <regex.h>
    #include"mosquitto.h"
    #include"utils.h"
    #include"telnet.h"
    
    
    //#define IP_ADDRESS   "127.0.0.1"
    #define IP_PORT      23
    #define SERV_PORT    3333
    #define MAXLINE      1024
    
    #define HOST "localhost"
    #define PORT  1883
    #define KEEP_ALIVE 60
    #define MSG_MAX_SIZE  512
    #define TOPIC_NUM 3
    #define Key_to_Fun_Size 3
    
    typedef struct sockaddr SA;
    typedef void (*Fun)() ;
    
    typedef struct key_word_to_fun
    {
    	char  key_word[MSG_MAX_SIZE];
    	Fun   fun;
        void* arg1;	
    }Key_to_Fun;
    
    
    static void log_in_telnet();
    
    
    bool session = true;
    uint32 SOCKFD = 0;
    char IP_ADDRESS[] = "xxx.xxx.xxx.xxx";
    
    const static char* topic[TOPIC_NUM] =
    {
    	"Gai爷:",
    	"ycy ",
    	"CCYY "
    };
    
    
    static Key_to_Fun tab_Key_to_Fun[Key_to_Fun_Size] =
    {
    	{"telnet",log_in_telnet,0},
    	{"login",log_in_telnet,0},
    	{"password",log_in_telnet,0},
    	//{NULL,NULL}
    };
    
    uint32 max(uint32 a, uint32 b)
    {
    	return (a>b?a:b);
    }
    
    void ERR_EXIT(char* s)
    {
    	perror(s);
    	exit(EXIT_FAILURE);
    }
    void INFO_PRINT(char* s)
    {
    	printf("%s",s);
    }
    
    uint32 match_string(char *src,char* dst,char *pattern)
    {
    	
        //char *pattern1 = "^telnet";
    	
    	char errbuf[MAXLINE];
    	//char match[MAXLINE];
    	regex_t reg;
    	int err,nm = 10;
    	regmatch_t pmatch[nm];
    
    	if(regcomp(&reg,pattern,REG_EXTENDED) < 0)
    	{
    		regerror(err,&reg,errbuf,sizeof(errbuf));
    		printf("err:%s
    ",errbuf);
    		return 0;
    	}
    	err = regexec(&reg,src,nm,pmatch,0);
    	if(err == REG_NOMATCH)
    	{
    		printf("no match
    ");
    		return 0;
    	}
    	else if(err)
    	{
    		regerror(err,&reg,errbuf,sizeof(errbuf));
    		printf("err:%s
    ",errbuf);
    		return 0;
    	}
    
    	for(int i=0;i<10 && pmatch[i].rm_so!=-1;i++)
    	{
    		int len = pmatch[i].rm_eo - pmatch[i].rm_so;
    		if(len)
    		{
    			memset(dst,'',MAXLINE);
    			memcpy(dst,src + pmatch[i].rm_so,len);
    			printf("%s
    ",dst);
    		}  
    	}
    	
    	return 1;
    
    }
    
    
    static void log_in_telnet()
    {
    	uint32 sockfd,isReady=0;
    	struct sockaddr_in servaddr;
    	uint32 hname[128];
    	
    	sockfd = socket(AF_INET,SOCK_STREAM,0);
    	bzero(&servaddr,sizeof(servaddr));
    	servaddr.sin_family = AF_INET;
    	servaddr.sin_port = htons(IP_PORT);
    	servaddr.sin_addr.s_addr = inet_addr(IP_ADDRESS);
    	printf("servaddr: IP is %s, Port is %d
    ",inet_ntoa(servaddr.sin_addr), ntohs(servaddr.sin_port));
    
    	while(connect(sockfd,(SA*)&servaddr,sizeof(servaddr))){};
    	SOCKFD = sockfd;
    	printf("connect has been ready
    ");
    }
    
    static void search_key_word_to_fun(struct mosquitto *mosq,char *s)
    {
    	int i = 0,j = 0;
    	char temp[MSG_MAX_SIZE] = {};
    	char match[MAXLINE];
    	char pattern[] = "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}";
    	
    	for(i =0 ;i< Key_to_Fun_Size;i++)
    	{
    		if(match_string(s,match,tab_Key_to_Fun[i].key_word))
    		{
    			if((i==0)&& (SOCKFD != 0))
    			{
    				printf("telnet has connect!! error command !!
    ");
    				return ;
    			}
    			else if((i==0)&& (SOCKFD == 0))
    			{
    			    if(match_string(s,match,pattern))
    				{
    					printf("---match------%s----------
    ",match);
    					for(j =0 ; match[j] != '' ;j++)
    					{
    						IP_ADDRESS[j] = match[j];
    					}
    					IP_ADDRESS[j] = '';
    					tab_Key_to_Fun[i].fun();
    				}
    				else
    				{
    					/* do nothing */
    					mosquitto_publish(mosq,NULL,"CCYY ",29,"you should put into your ip
    ",0,0);
    				}
    				
    			}
    			else
    			{
    				tab_Key_to_Fun[i].fun();
    			}
    			return ;
    		}
    	}
    	if((SOCKFD)&&(i != 0))
    	{
    		writen(SOCKFD,s,strlen(s));
    	}
    }
    
    static void my_message_callback(struct mosquitto *mosq, void *userdata, const struct mosquitto_message *message)
    {
    	
        if(message->payloadlen){
            printf(">%s %s", message->topic, (char *)message->payload);
    		search_key_word_to_fun(mosq,(char *)message->payload);
    		
    		
    		//MQTT发啥 就往telnet发啥
        }else{
            printf("%s (null)
    ", message->topic);
        }
        //fflush(stdout);
    }
    
    static void my_connect_callback(struct mosquitto *mosq, void *userdata, int result)
    {
        int i;
        if(!result){
            /* Subscribe to broker information topics on successful connect. */
            mosquitto_subscribe(mosq, NULL, "ycy ", 2);
        }else{
            fprintf(stderr, "Connect failed
    ");
        }
    }
    
    static void my_subscribe_callback(struct mosquitto *mosq, void *userdata, int mid, int qos_count, const int *granted_qos)
    {
        int i;
        printf("Subscribed (mid: %d): %d", mid, granted_qos[0]);
        for(i=1; i<qos_count; i++){
            printf(", %d", granted_qos[i]);
        }
        printf("
    ");
    }
    
    static void my_log_callback(struct mosquitto *mosq, void *userdata, int level, const char *str)
    {
        /* Pring all log messages regardless of level. */
        printf("%s
    ", str);
    }
    
    void wait_message_from_telnet(struct mosquitto *mosq,FILE *fp,uint32 sockfd)
    {
    	uint32 maxfdp1,nready;//stdineof;
    	fd_set rset;
    	uint8 buf[MAXLINE];
    	uint8 respbuff[MAXLINE] = {0};;
    	uint32 resplen;
    	uint32 n;
    	uint8 echo_cmd[] = {0xff,0xfb,0x01};
    	//stdineof = 0;
    	FD_ZERO(&rset);
    	writen(sockfd,echo_cmd,3);
    	
    	for(;;)
    	{
    		//if(stdineof == 0)
    		FD_SET(fileno(fp),&rset);
    		FD_SET(sockfd,&rset);
    		maxfdp1 = max(fileno(fp),sockfd)+1;
    		nready = select(maxfdp1,&rset,NULL,NULL,NULL);
    		
    		if(nready < 0)
    		{
    			ERR_EXIT("ERROR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    		}
    		
    		if(FD_ISSET(sockfd,&rset))
    		{
    			memset(buf,0,MAXLINE);
    			if((n = read(sockfd,buf,MAXLINE))==0)
    			{
    				ERR_EXIT("str_cli:server termination prematurely");
    			}
    			buf[n] = '';
    			//printf("FD_ISSET(sockfd,&rset)------11------%s----22----------------
    ",buf);
    			
    			if(buf[0] == IAC)
    			{
    				memset(respbuff,0,MAXLINE);
    				resplen = get_every_frame(buf,n,respbuff,MAXLINE);
    				writen(sockfd,respbuff,resplen);
    			}
    			else
    			{
    				writen(fileno(stdout),(char *)buf,n);//当数据比较大的时候 会造成发给cloud数据错位
    				//printf("------------2------------
    ");
    				mosquitto_publish(mosq,NULL,"CCYY ",strlen(buf)+1,buf,0,0);
    				//printf("------------3------------
    ");
    				memset(buf,0,MAXLINE);
    			}
    			
    			//writen(fileno(stdout),buf,n);
    		}
    		if(FD_ISSET(fileno(fp),&rset))
    		{
    			memset(buf,0,MAXLINE);
    			if((n = readline(fileno(fp),(char *)buf,MAXLINE)) == 0)
    			{
    				//stdineof = 1;//此时碰到EOF 并且马上要发生FIN序列 所以标准输入不可读了
    				shutdown(sockfd,SHUT_WR);
    				FD_CLR(fileno(fp),&rset);
    				INFO_PRINT("nothing input!");
    				continue;
    			}
    			else if(n >0)
    			{
    				/* do nothing */
    			}
    			else
    			{
    				ERR_EXIT("some error occurred ");
    			}
    			//printf("FD_ISSET(fileno(fp),&rset)----%d--
    ",n);
    			//memset(buf,0,MAXLINE);
    			writen(sockfd,(char *)buf,n);
    			//send_message_to_cloud(sockfd,(char *)buf,n);
    		}
    	}
    }
    
    int main()
    {
        struct mosquitto *mosq = NULL;
    	char buff[MSG_MAX_SIZE];
    	
        //libmosquitto 库初始化
        mosquitto_lib_init();
        //创建mosquitto客户端
        mosq = mosquitto_new(NULL,session,NULL);
        if(!mosq){
            printf("create client failed..
    ");
            mosquitto_lib_cleanup();
            return 1;
        }
        //设置回调函数,需要时可使用
        //mosquitto_log_callback_set(mosq, my_log_callback);
        mosquitto_connect_callback_set(mosq, my_connect_callback);
        mosquitto_message_callback_set(mosq, my_message_callback);
        mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
    	
    
        //连接服务器
        if(mosquitto_connect(mosq, HOST, PORT, KEEP_ALIVE)){
            fprintf(stderr, "Unable to connect.
    ");
            return 1;
        }
        //开启一个线程,在线程里不停的调用 mosquitto_loop() 来处理网络信息
        int loop = mosquitto_loop_start(mosq); 
        if(loop != MOSQ_ERR_SUCCESS)
        {
            printf("mosquitto loop error
    ");
            return 1;
        }
    
    	while(!SOCKFD){};
    	wait_message_from_telnet(mosq,stdin,SOCKFD);
    	
    
        mosquitto_destroy(mosq);
        mosquitto_lib_cleanup();
    
        return 0;
    }
    
    
    
    

    一下是程序运行情况

    总结:
    1.gdb调试方法十分有效 应当加强掌握
    2.在调试过程中出现过程序不稳定的情况 但是很偶然 根据那几次的分析结果 可能是读写缓冲区的问题 比如发来的数据很快并且多 这时候读数据的速度跟不上 就会出现显示出现问题 这个方面我不是很了解 以后要了解一下这方面的知识

    以上

  • 相关阅读:
    HDU2515_数学规律题
    HDU1086_You can Solve a Geometry Problem too_判断两线段相交
    HDU1115_Lifting the Stone_凹凸多边形重心_可作为模板
    HDU2036_改革春风照大地_点求多边形面积
    Codeforces Beta Round #92 (Div. 2 Only) _A题
    HDU2108_Shape of HDU_判断凹凸
    response.setContentType设置
    vue 文件下载实现
    iText5实现Java生成PDF文件完整版
    java使用IText将数据导出为pdf文件(数据为excel表格样式)
  • 原文地址:https://www.cnblogs.com/y-c-y/p/11692632.html
Copyright © 2011-2022 走看看