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

📄 main.c

📁 基于UNIX的中间件编程,只能运行于UNIX操作系统
💻 C
📖 第 1 页 / 共 5 页
字号:
{/*	char m_rectime[11];	SYSTEMTIME lpSystemTime;	memcpy(m_rectime,m_centertime+3,4);	m_rectime[4]='\0';	lpSystemTime.wYear=atoi(m_rectime);	memcpy(m_rectime,m_centertime+7,2);	m_rectime[2]='\0';	lpSystemTime.wMonth=atoi(m_rectime);	memcpy(m_rectime,m_centertime+9,2);	m_rectime[2]='\0';	lpSystemTime.wDay=atoi(m_rectime);	memcpy(m_rectime,m_centertime+11,2);	m_rectime[2]='\0';	lpSystemTime.wHour=atoi(m_rectime);	memcpy(m_rectime,m_centertime+13,2);	m_rectime[2]='\0';	lpSystemTime.wMinute=atoi(m_rectime);	memcpy(m_rectime,m_centertime+15,2);	m_rectime[2]='\0';	lpSystemTime.wSecond=atoi(m_rectime);	if(SetLocalTime(&lpSystemTime))		printf("set localtime success write logfile\n");	else		printf("set localtime error the errorno is %d\n",WSAGetLastError());*/	}/*--------------------------------------------------------------RepacleChar():replace the char--------------------------------------------------------------*/void replacechar(char *m_sourstr,int m_reschar,int m_deschar){	unsigned int i;	char *m_temsour;	m_temsour=m_sourstr;	for(i=0;i<strlen(m_sourstr);i++)	{		if(*m_temsour==m_reschar)			*m_temsour=m_deschar;		m_temsour++;	}}/*-----------------------------------------------Timer()function:dispose the local tasks -----------------------------------------------*/void *Timer(void *arg){/*---------------------------------------------step 1:initialize the system and variables	---------------------------------------------*/	int TimeOutCount;	/*	DWORD t_time;	DWORD b_time;	*/	time_t t_time,b_time;	char Buffer[22];	char TestBuffer[200];	unsigned int loopflag;	unsigned loop;	int loopcount ;	unsigned sendcount;	int ioctl_opt;	int current_ser;	int maxloop;	int socket_type ;	struct sockaddr_in server;	struct sockaddr_in local ;	struct sockaddr_in RemoteServer;	int RemoteServerLen;	int i;	SOCKET  Ssock;	WSLOGDATA m_log;		fd_set readfds, writefds, exceptfds;	int nfds;	struct timeval z_time;	char temp[34];	char *port;	char result[22];	char oldresult[22];	socket_type = DEFAULT_PROTO;	sendcount=0;	TimeOutCount=0;	loopcount =0;	loop=1;	loopflag=0;	maxloop=-1;	ioctl_opt=1;	RemoteServerLen=sizeof(RemoteServer);	nfds=getdtablesize();		FD_ZERO(&readfds);	FD_ZERO(&writefds);	FD_ZERO(&exceptfds);		m_log=SetLogContent(L_Info,"the local thread begin success","the  timer thread  begin to run");	writeLog(&m_log);			z_time.tv_sec=0;	z_time.tv_usec=300;			memset(&server,'\0',sizeof(server));	server.sin_addr.s_addr=inet_addr(remote_ser[1].RemoteIP);	server.sin_family=AF_INET; 	server.sin_port=htons(remote_ser[1].Rem_testPort);	ser_testres[1].RemoteID=remote_ser[1].RemoteID;	ser_testres[1].RemoteLevef=remote_ser[1].RemoteLevef;	ser_testres[1].RemoteStatus=0;	printf("serverinfo:%d,%d,%s\n",server.sin_addr.s_addr,server.sin_port,remote_ser[1].RemoteIP);			memset(&local,0,sizeof(local));	local.sin_addr.s_addr=inet_addr(myname.IP);	local.sin_family =AF_INET; 	local.sin_port =htons(myname.MYSECONDPORT);		RemoteServer.sin_family =AF_INET;		printf("Local host information:%d,%d %s\n",local.sin_addr.s_addr,local.sin_port,myname.IP);		/*-------------------------------------------step 2:connect to the UDP server-------------------------------------------*/	port=temp;	itoa(myname.MYSECONDPORT ,temp,10);		Ssock=passiveUDP(myname.IP ,port);	if (Ssock==INVALID_SOCKET)	{		char temp[100];		fprintf(stderr,"local host  Opening socket: Error %d\n",WSAGetLastError());		sprintf(temp,"with error %d\0",WSAGetLastError());		m_log=SetLogContent(L_Error,"timer thread create socket failed",temp);		writeLog(&m_log);		return ;	}/*----------------------------------------------------------stem 3:send the request to determine the networking status----------------------------------------------------------*/	FD_SET(Ssock,&readfds);	FD_SET(Ssock,&writefds);	FormateTestPacket(Buffer,"RQS");	Buffer[18]='\0';  	WriteMessage(Ssock, Buffer,strlen(Buffer),server,sizeof(server));/*	t_time=GetTickCount();*/	t_time=mktime(getlocaldatetime());/*-------------------------------------------------------------stemp 4:loop in send and receive to determine the networking status-------------------------------------------------------------*/	memset(TestBuffer,'\0',sizeof(TestBuffer));	while(1)	{			FD_CLR(Ssock,&readfds);		FD_CLR(Ssock,&writefds);		FD_ZERO(&readfds);		FD_ZERO(&writefds);		FD_SET(Ssock,&writefds);		FD_SET(Ssock,&readfds);		i = select(nfds,&readfds,&writefds,&exceptfds,&z_time);				if (i == SOCKET_ERROR)		{			char temp[100];			TimeOutCount++;			printf(">>local host select failed %d\n",WSAGetLastError());			m_log.log_level=10;			sprintf(temp,"with error %d\0",WSAGetLastError());			m_log=SetLogContent(L_Error,"select failed",temp);			writeLog(&m_log);			if(TimeOutCount>20)			{				m_log=SetLogContent(L_Error,"select timeout :6s","the local \					thread find a network error according to timeout and set \					the networkstatus to off");				writeLog(&m_log);								NetworkStatus=Network_Off;							printf(">>becuase timeout ,an network error be fund\n");							}			continue;		}				if (i==0)		{			TimeOutCount++;			printf(">>local host Select returned no fds ready\n");			if(TimeOutCount>20)			{				m_log=SetLogContent(L_Error,"select timeout :6s","the local \					thread find a network error according to timeout and set \					the networkstatus to off");				writeLog(&m_log);								NetworkStatus=Network_Off;				printf(">>becuase timeout ,an network error be fund\n");							}			continue;		}				if (FD_ISSET(Ssock,&readfds)!=0 )		{			memset(TestBuffer,'\0',sizeof(TestBuffer));			if (ReadAndEcho(Ssock,TestBuffer,sizeof (TestBuffer),&RemoteServer,&RemoteServerLen))			{				printf(">>The server's timer thread teminate communication\n");				m_log=SetLogContent(L_Unknown,"the readandecho function failed","not \					known");				writeLog(&m_log);				FD_CLR(Ssock,&readfds);				FD_CLR(Ssock,&writefds);				FD_SET(Ssock,&writefds);				FD_SET(Ssock,&readfds);				continue;			}			else			{				if(NetworkStatus==Network_Off)					NetworkStatus=Network_On;								TimeOutCount=0;				loop=1;					if(RemoteServer.sin_addr.s_addr!=local.sin_addr.s_addr)				{					current_ser =loop;					sendcount=0;					ser_testres[current_ser].RemoteStatus = 1;										if(strstr(TestBuffer,"RQS")!=NULL)					{						printf(">>network is on and send ack to server\n");						memset(Buffer,'\0',sizeof(Buffer));						FormateTestPacket(Buffer,"ACK");						WriteMessage(Ssock, Buffer,strlen(Buffer),RemoteServer,sizeof(RemoteServer));					}					else					{						/*						cmp_time(TestBuffer);																*//*						b_time=GetTickCount();						if(abs(b_time-t_time)>3000)*/						b_time=mktime(getlocaldatetime());						if(difftime(b_time,t_time)>3000)						{							printf(">>network is on and send REQ to server\n");							memset(Buffer,0,sizeof(Buffer));							FormateTestPacket(Buffer,"RQS");							if (WriteMessage(Ssock, Buffer,strlen(Buffer),server,sizeof(server))<=0) 							{								m_log=SetLogContent(L_Error,"writeMessage function \									error","in  the timer thread ,when send req ,an \									error occured");								writeLog(&m_log);								printf(">>the server's timerthread terminate connection\n");								FD_CLR(Ssock,&readfds);								FD_CLR(Ssock,&writefds);								FD_SET(Ssock,&writefds);								FD_SET(Ssock,&readfds);								continue;							}							t_time=b_time;						}/*compare time end*/					}/*send REQ end*/				}/*determin server address end*/			}/*read correct end*/		}/*the read sock ready end*/		memset(result,0,sizeof(result));	memset(oldresult,0,sizeof(oldresult));	/*	b_time=GetTickCount();	if(abs(b_time-t_time)>6000)	*/	b_time=mktime(getlocaldatetime());	if(difftime(b_time,t_time)>6000)		{			if(sendcount>=4)			{					NetworkStatus=Network_Off;					printf(">>the server not reaponse,maybe the network down\n");					ser_testres[loop].RemoteStatus = 0;					sendcount=0;					printf(">>the local hosts test server%d terminating communication timeout\n",loop);					m_log=SetLogContent(L_Error,"client test terminate"," the test \						thread cannot send packet maybe network error");					writeLog(&m_log);                                         			}			memset(Buffer,0,sizeof(Buffer));			FormateTestPacket(Buffer,"RQS");			if (WriteMessage(Ssock, Buffer,strlen(Buffer),server,sizeof(server))<=0) 			{				m_log=SetLogContent(L_Error,"writeMessage function error", "in \					the timer thread ,when send req(not first req) ,an error \					occured");				writeLog(&m_log);				printf(">>the server's timer thread terminate communication\n");				FD_CLR(Ssock,&readfds);				FD_CLR(Ssock,&writefds);				FD_SET(Ssock,&writefds);				FD_SET(Ssock,&readfds);				continue;			}			else			{				t_time=b_time;				sendcount++;					printf(">>sendcount[%d]=%d\n",loop,sendcount);			}		}					}/*end while*/closesocket(Ssock);return ;}/*-------------------------------------------------------------------------NetworkThread():this is the network thread.It 's function is send and recieve the message-------------------------------------------------------------------------*/void *NetworkThread(void *arg ){	WSLOGDATA m_log;	int cc;	struct sockaddr_in fsin;	int alen;	char buf[MAXLEN];	int oprcode;	char packettype[2];	struct timeval z_time;		/* ----------------------------------------------  create the cm--cm  passive socket----------------------------------------------*/	SOCKET Msock;/*cm--cm main port*/	SOCKET Ssock;/*connect to the app's passive port*/	SOCKET Qsock;/*connect to the server's queue thread passive socket	*/	char temp[34];	char *port;	port=temp;	z_time.tv_sec=0;	z_time.tv_usec=300;	alen=sizeof(fsin);	itoa(myname.MYMAINPORT ,temp,10);	Msock=passiveUDP(myname.IP ,port);	if(Msock==INVALID_SOCKET)	{		printf("network thread:cannot create the cm--cm main socket\n");		m_log=SetLogContent(L_Error,"cannot create the socket","when call the Passive \				UDP functin ,the cm-cm main socke cannot be created");		writeLog(&m_log);		return ;	}	else	{		printf(" network thread:created the cm--cm main socket\n");		m_log=SetLogContent(L_Success," create the socket","when call the Passive \			UDP functin ,the cm-cm main socke  be created successfully");		writeLog(&m_log);	}	Ssock=connectUDP(myname.IP ,AppSocket);	if(Ssock==INVALID_SOCKET)	{		printf("network thread:cannot connect to the app--cm socket\n");		m_log=SetLogContent(L_Error,"cannot create the socket","when call the connectUDP functin ,the app-cm socke cannot be created");		writeLog(&m_log);		return ;	}	else	{		printf("network thread:connect to the local cm server port successfully\n");		m_log=SetLogContent(L_Success,"connect socket successfully","when call the connectUDP \				 functin ,the return socke  be created successfully");		writeLog(&m_log);	}	Qsock=connectUDP(remote_ser[1].RemoteIP,QueueMsg);	if(Qsock==INVALID_SOCKET)	{		printf("network thread:cannot connect to the queuethread's paasive socket\n");		m_log=SetLogContent(L_Error,"cannot create the socket","when call the connectUDP functin ,the queuethread socke cannot be created");		writeLog(&m_log);		return ;	}	else	{		printf("network thread:connect to the server's queue port successfully\n");		m_log=SetLogContent(L_Success,"connect socket successfully","when call the connectUDP functin ,the return socke  be created successfully");		writeLog(&m_log);	}	while(1)	{		memset(buf,'\0',sizeof(buf));		printf(">>network thread  accept the middleware's request...\n");		cc=recvfrom(Msock,buf,sizeof(buf),0,(struct sockaddr *)&fsin,&alen);		if(cc<=0)/*when recieve 意外错误*/		{	/*			sendto(Msock,(char *)E_Other,sizeof((char)E_Other),0,(struct sockaddr *)&fsin,alen);	*/

⌨️ 快捷键说明

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