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

📄 sndmsg.c

📁 linux下c 编写的 某项目代理服务端部分的代码。其中包括网络通信
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <varargs.h>#include <unistd.h>#include <time.h>#define SIZE 256 #define SIZE1 30void p_debug();void m_debug();void getltime();main(){  char buf[SIZE];  FILE *fp,*fp1;  int  len,count,n;  char *ptr;  char *desttmp;  char status[10],result[100];  char time[80];  char alarm_site[30],alarm_date[30],alarm_time[30];  char tmp0[30],tmp1[30],tmp2[30],tmp3[30],tmp4[30];   char tmp5[30],tmp6[30],tmp7[30],tmp8[30],tmp9[30];   char tmp10[30],tmp11[30],tmp12[30];  n=0;  memset(buf,0x0,SIZE);  memset(status,0x0,sizeof(status));  memset(result,0x0,sizeof(result));  memset(tmp0,0x0,SIZE1);  memset(tmp1,0x0,SIZE1);  memset(tmp2,0x0,SIZE1);  memset(tmp3,0x0,SIZE1);  memset(tmp4,0x0,SIZE1);  memset(tmp5,0x0,SIZE1);  memset(tmp6,0x0,SIZE1);  memset(tmp7,0x0,SIZE1);  memset(tmp8,0x0,SIZE1);  memset(tmp9,0x0,SIZE1);  memset(tmp10,0x0,SIZE1);  memset(tmp11,0x0,SIZE1);  memset(tmp12,0x0,SIZE1);  desttmp=(char *)malloc(sizeof(char)*3);    while(1)    {	if (fgets(buf,256,stdin)==NULL){		getltime(time);		m_debug("%s fgets NULL data\n",time);		/*if(n==2){		printf("n=%d\n",n);		n=0;		system("ksh /usr/csc/src/pp1.sh");		printf("wait!\n");		} 11-29 add :if{}*/		n++;		sleep(1);		continue;		}	else{	if (buf[0]=='#'){	sscanf(buf,"%s%s%10s%s%s%s",tmp0,tmp1,tmp2,tmp3,tmp4,tmp5);	sprintf(status,"%10s|",tmp2);	strcpy(result,status);	count=1;	}	if ((count==1)&&(strstr(buf,"environmentFailureEvent")!=NULL)) {	sscanf(buf,"%s%s%s%s%s%s%s%s%s%s%s%s",tmp0,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6,tmp7,tmp8,tmp9,tmp10,tmp11);	if (strlen(tmp11)==0){		if (strlen(tmp9)<10){		strcpy(tmp11,tmp9);		strcpy(tmp9,"0");		strcat(tmp9,tmp11);  	memset(tmp11,0x0,SIZE1);		}		sprintf(tmp0,"%-30s|%-10s%s|",tmp4,tmp9,tmp10);		strcat(result,tmp0);  	memset(tmp0,0x0,SIZE1);  	memset(tmp4,0x0,SIZE1);  	memset(tmp9,0x0,SIZE1);  	memset(tmp10,0x0,SIZE1);	    }	else{		strcat(tmp4,tmp5);		if (strlen(tmp10)<10){		strcpy(tmp12,tmp10);		strcpy(tmp10,"0");		strcat(tmp10,tmp12);  		memset(tmp12,0x0,SIZE1);		}		sprintf(tmp0,"%-30s|%-10s%s|",tmp4,tmp10,tmp11);		strcat(result,tmp0);		strcat(result,"\n");  	memset(tmp0,0x0,SIZE1);  	memset(tmp4,0x0,SIZE1);  	memset(tmp5,0x0,SIZE1);  	memset(tmp9,0x0,SIZE1);  	memset(tmp10,0x0,SIZE1);  	memset(tmp11,0x0,SIZE1);	   }	count=2;        }	if ((count==2)&&((desttmp=strstr(buf,"(0)"))!=NULL||\	(desttmp=strstr(buf,"(1)"))!=NULL||\	(desttmp=strstr(buf,"(2)"))!=NULL||\	(desttmp=strstr(buf,"(3)"))!=NULL||\	(desttmp=strstr(buf,"(4)"))!=NULL||\	(desttmp=strstr(buf,"(5)"))!=NULL||\	(desttmp=strstr(buf,"(6)"))!=NULL||\	(desttmp=strstr(buf,"(7)"))!=NULL||\	(desttmp=strstr(buf,"(8)"))!=NULL||\	(desttmp=strstr(buf,"(9)"))!=NULL||\	(desttmp=strstr(buf,"(10)"))!=NULL||\	(desttmp=strstr(buf,"(11)"))!=NULL)){	sprintf(tmp0,"%3s",desttmp);	strcat(result,tmp0);	strheader(result);		p_debug("result=[%s]\n",result);	if (snddata(result)<0)		m_debug("snd data failure\n");	memset(result,0,sizeof(result));	}   	memset(buf,0x0,SIZE);  	}    }}int strheader(char *buf){	char  *strhead;	char  tmp[256];		strhead="02011001120011";	strcpy(tmp,buf);	strcpy(buf,strhead);	strcat(buf,tmp);}		#define SNDPORT 7201int snddata(char *buff){   struct sockaddr_in server;   int tmpsock,flag = 1,i,errno;   int wrtlen;       if ((tmpsock = socket(AF_INET,SOCK_STREAM,0))<0){	m_debug("init tmpsock error\n");      	return(-1);	}   server.sin_family = AF_INET;   server.sin_port = htons(SNDPORT);   server.sin_addr.s_addr = inet_addr("192.168.89.3");   /*connect to server*/  if(connect(tmpsock,(struct sockaddr *)&server,sizeof(struct sockaddr))<0){	m_debug("connect to server error\n");         close(tmpsock);         return(-3);   }       else{	wrtlen=write(tmpsock,buff,strlen(buff));	if (wrtlen)	close(tmpsock);	else{	m_debug("snd data fail\n");	return(-1);	}	}}#define	TEMPDIR	"/home/csc/print"#define TRUE	1void p_debug(va_alist)va_dcl{	va_list args;	char    *fmt;	FILE    *fp;	char filename[30], *ttyname(), *tname;	tname = ttyname(TRUE);	sprintf(filename, "%s/sndapp", TEMPDIR);	fp = fopen(filename, "a");	va_start(args);	fmt = va_arg(args, char *);	vfprintf(fp, fmt, args);	va_end(args);	fclose(fp);}void m_debug(va_alist)va_dcl{	va_list args;	char 	*fmt;	FILE	*fp;	char filename[30],*ttyname(),*tname;	tname=ttyname(TRUE);	sprintf(filename,"%s/snderr",TEMPDIR);	fp=fopen(filename,"a");	va_start(args);	fmt=va_arg(args,char *);	vfprintf(fp,fmt,args);	va_end(args);	fclose(fp);}void getltime(char *tmp){	time_t	tv;		tv=time((time_t *) 0);		strcpy(tmp,ctime(&tv)+4);	tmp[20]=0;}	

⌨️ 快捷键说明

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