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

📄 tcpc.c

📁 Linux下的C编程
💻 C
字号:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <netdb.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> 
#include <sys/stat.h>#ifndef false
	#define false	0
#endif

#ifndef true
	#define true	1
#endiftypedef int	bool;int main(int argc, char *argv[]) { 	int i=0;    	int sockfd, numbytes;  	char datafile[256];	struct stat st;
	bool isExist = false;
	int size;	int length;
	char file_buffer[4096];
	int ai_number,di_number;    	struct hostent *he;     	struct sockaddr_in their_addr;     	int myport;     	char *home = getenv("CEMS");	char config[256];	strcpy(config,home);	printf("1config is %s\n",config);	strcat(config,"/src/TCPrt/tcpc.ini");	printf("2config is %s\n",config);	char ip[20],rtpath[256];	int cycle=10;	strcpy(rtpath,home);	strcat(rtpath,"/data");	printf("rtpath is %s\n",rtpath);	FILE *fpini;	char buffport[20],buffcycle[10];
	if((fpini=fopen(config,"r"))!=NULL)	{		fscanf(fpini,"%s\n",ip);		printf("ip is %s\n",ip);		fscanf(fpini,"%s\n",buffport);		myport=atoi(buffport);		printf("myport is %d\n",myport);		//fscanf(fpini,"%s",buffcycle);		//cycle==atoi(buffcycle);		printf("cycle is %d\n",cycle);		fclose(fpini);
	}		fflush(stdout);	if((he=gethostbyname(ip))==NULL) 	{         	herror("gethostbyname");         	exit(1); 	}	FILE *fp;	i=0;	while(1)	{		if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) 		{         		printf("socket error!\n"); 			fflush(stdout);			//exit(1);        		sleep(cycle);        		continue;     		}     		their_addr.sin_family=PF_INET;     		their_addr.sin_port=htons(myport);     		their_addr.sin_addr = *((struct in_addr *)he->h_addr);     		bzero(&(their_addr.sin_zero),0);     		if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1)		{         		//perror("connect");         		printf("connetct failed!");			fflush(stdout);        		sleep(cycle);        		//exit(1);         		continue;    		} 		printf("ok!");			//prepare send data buffer		i++;
			isExist = false;			sprintf(datafile,"%s/realtime%d.txt",rtpath,i);			printf("datafile is %s\n",datafile);			fflush(stdout);
			if(stat(datafile,&st)==0) 			{ //file exist.
				isExist = true;
				size = st.st_size;			}			else			{				if(i>4)				{					i=0;				}				sleep(cycle);				continue;			}
			bzero(&st,sizeof(struct stat));				if(isExist)			{				bzero(file_buffer,4096);
				//according save format to load data.
				FILE *fp;
				if((fp=fopen(datafile,"rb"))!=NULL){
					fread(file_buffer,size,1,fp);
					fclose(fp);
				}				char PacketType[5];
				memcpy(PacketType,file_buffer,4);        			PacketType[4]=0;				printf("PacketType is %s\n",PacketType);				fflush(stdout);
				if(strcmp(PacketType,"REAL")!=0){
					printf("REAL is error \n");					fflush(stdout);					break;
				}				memcpy(&ai_number,file_buffer+20,4);
				memcpy(&di_number,file_buffer+24,4);
				length = 30 + ai_number*4 + di_number*1;				printf("length is %d\n",length);				fflush(stdout);			}	    		if(write(sockfd,file_buffer,length)==-1)    			{          			fprintf(stderr,"Write Error:%s\n",strerror(errno));          			exit(1);     			}    			printf("write finished!\n");    			/*if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {         			perror("recv");         			exit(1);     			}     			printf("Received: %s\n",buf); */    		close(sockfd); 		if(i>=4)		{			sleep(cycle);			i=0;		}		else		{			sleep(1);		}		printf("xxx!\n"); 	}    	return 0; } 

⌨️ 快捷键说明

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