⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 基于linux下C语言实现的CMPP通信协议
💻 C
字号:
#include <dlfcn.h>#include <mysql.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <unistd.h>#include <fcntl.h>#include <sys/stat.h>#include <unistd.h>#include <pthread.h>#include <semaphore.h>#include <errno.h>/*#include "global.h"#include "md5.h"#include "code.h"#include "fifo.h"*/#include "cmpp/cmpp.h"#include "main.h"#include "conf.h"void send_thread();void recv_thread();static MYSQL db;static int recv_run,send_run;struct conf cfg[10];int main(){	int ret=0;	/* load cmpp.so */	void *handle=dlopen("./cmpp/cmpp.so",RTLD_LAZY);	if(!handle)	{		printf("Can't load cmpp.so\n");		exit(0);	}	dl_cmpp_init=dlsym(handle,"cmpp_init");	dl_cmpp_exit=dlsym(handle,"cmpp_exit");	dl_cmpp_connect=dlsym(handle,"cmpp_connect");	dl_cmpp_terminate=dlsym(handle,"cmpp_terminate");	dl_cmpp_active_test=dlsym(handle,"cmpp_active_test");	dl_cmpp_submit=dlsym(handle,"cmpp_submit");	dl_cmpp_deliver_resp=dlsym(handle,"cmpp_deliver_resp");	dl_cmpp_has_mo=dlsym(handle,"cmpp_has_mo");	dl_cmpp_get_mo=dlsym(handle,"cmpp_get_mo");	/* load conf */	if(conf_load("./sms.conf",cfg))	{		printf("Can't load config file\n");		exit(1);	}	/* connect mysql */	if(!mysql_real_connect(&db,		 conf_getvalue(cfg,"mysql_ip"),		 conf_getvalue(cfg,"mysql_uid"),		 conf_getvalue(cfg,"mysql_pwd"),		 conf_getvalue(cfg,"mysql_name"),		 atoi(conf_getvalue(cfg,"mysql_port")),		 NULL,		 0)) 	{ 		printf("Connect mysql error\n");		printf(mysql_error(&db));		exit(1);	}	/* connect cmpp */	printf("china mobile peer to peer (cmpp)\n");	printf("code by jiniang at 2005.1.31\n");	printf("Please press \"Enter\" key to continue\n");	getchar();	//ret=dl_cmpp_init("192.168.0.2",7890,1000);	ret=dl_cmpp_init(conf_getvalue(cfg,"cmpp_ip"),atoi(conf_getvalue(cfg,"cmpp_port")),1000);	if(ret)	{		printf("init error %d\n",ret);		dl_cmpp_exit();		exit(1);	}	//ret=dl_cmpp_connect("123456","123456");		ret=dl_cmpp_connect(conf_getvalue(cfg,"cmpp_uid"),conf_getvalue(cfg,"cmpp_pwd"));	if(ret)	{		printf("cmpp connnect error %d\n",ret);		dl_cmpp_exit();		exit(1);	}	/* init send & receive thread */	pthread_t send_pid,recv_pid;	recv_run=1;	ret=pthread_create(&recv_pid,NULL,(void*)recv_thread,NULL);	if(ret)	{			printf("create recv_thread error\n");			exit(1);	}	send_run=1;	ret=pthread_create(&send_pid,NULL,(void*)send_thread,NULL);	if(ret)	{			printf("create send_thread error\n");			exit(1);	}	printf("Enter 'q' to quit\n");	while(1)	{		char quit=getchar();		if(quit=='q' ||quit=='Q') break;	}	send_run=0;	recv_run=0;	pthread_join(send_pid,NULL);	pthread_join(recv_pid,NULL);	dl_cmpp_exit();	dlclose(handle);	return 0;}void recv_thread()	//save deliver to income{	char sql[512]="";	while(recv_run)	{		if(!dl_cmpp_has_mo(1000)) continue;		char mo[300];		memset(mo,0,300);		int ret=0;		ret=dl_cmpp_get_mo(mo);		switch(ret)		{			case 0:			break;			{				printf("get empty !!!\n");				break;			}			case 1:			{				struct cmpp_deliver *pmo=(struct cmpp_deliver*)mo;				printf("[D] %s->%s:%s\n",pmo->dest_id,pmo->src_terminal_id,pmo->msg_content);				sprintf(sql,					"insert into income(usernum,spnum,context,regtime) values ('%s','%s','%s',now())",					pmo->dest_id,					pmo->src_terminal_id,					pmo->msg_content);				if(mysql_real_query(&db,sql,strlen(sql)))				{					printf("mysql_real_query error\n");					printf(mysql_error(&db));				}				break;			}			case 2:			{				struct cmpp_report *pmo=(struct cmpp_report*)mo;				char stat[]="\0\0\0\0\0\0\0";				memcpy(stat,pmo->stat,7);				printf("[R] %s:%s\n",pmo->dest_terminal_id,stat);				break;			}		}	}}void send_thread()	//get easysend to send sms{	char sql[255]="";	char *search="select * from easysend where state=0 limit 10";	int i=0;	int ret=0;	while(send_run)	{		MYSQL_RES *res;		MYSQL_ROW row;		if(mysql_real_query(&db,search,strlen(search)))		{			printf("mysql_real_query error\n");			printf(mysql_error(&db));		}		res=mysql_store_result(&db);		while(1)		{			row=mysql_fetch_row(res);			if (row==NULL) break;			char msg_id[]="\0\0\0\0\0\0\0\0";			sprintf(sql,"update easysend set state=1 where id=%s",row[0]);			mysql_real_query(&db,sql,strlen(sql));			ret=dl_cmpp_submit(msg_id,	//msg_id						1,	//pk_total						1,	//pk_number						1,	//registerred_elivery						1,	//msg_level						"test",//service_id						3,	//fee_usertype						"",	//fee_terminal_id						0,	//pid						0,	//udhi						15,	//msg_fmt(dcs)						conf_getvalue(cfg,"cmpp_uid"),//msg_src(sp_id)						"02",	//feetype						"000000",//feecode						"",	//valid_time						"",	//at_time						"01234",	//src_id						1,						row[1],						strlen(row[2]),						row[2],						"\0\0\0\0\0\0\0");			if(!ret)			{				printf("[S] 01234->%s:%s OK\n",row[1],row[2]);				//update state =1			}			else			{				//update state=-1				printf("[S]01234->%s:%s Error\n",row[1],row[2]);			}		}		mysql_free_result(res);		struct timeval tv;		tv.tv_sec=03;			//3 s timeout		tv.tv_usec=0;		select(0,NULL,NULL,NULL,&tv);	//thread sleep	}}

⌨️ 快捷键说明

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