📄 sm_text_down.c
字号:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <net/if_arp.h>
#include <netdb.h>
#include "ConfigInfo.h"
#include "SmBsString.h"
#include "Vcrc.h"
#include "SM_Text_Down.h"
extern char DownLoadingName[textline];
extern struct VedioPLNumber Vplnumber;
extern char TotalSizeBuf[128];
extern char percent[40];
extern int TotalSize;
char *DataCrc32;
char Uppl[MaxReceiveBufSize];
char Upml[MaxReceiveBufSize];
int retv;
char *PLname[textline];
int ServPort2;
int BufSize=655350 ;
int VisitWsIntervalSeconds ;
int SavePlFlag;
int SaveMlFlag;
char *ipaddr;
char TotalVedioProgram[MaxReceiveBufSize];
char TempLateHeadBuf[MaxReceiveBufSize];
char TempLateBodyBuf[MaxReceiveBufSize];
char PicHeadBuf[MaxReceiveBufSize];
char PicBodyBuf[MaxReceiveBufSize];
char temp[MaxReceiveBufSize];
char tempchar2[BUFSIZE1];
char Appath[MiddleBufSize];
char DnLoadingname[128];
char Buf[14];
char Buff[129];
char c1=(char)14;
char c2=(char)15;
char c3=(char)18;
char c4=(char)19;
char c5=(char)20;
char c6=(char)16;
char c7=(char)17;
ExsitList Exsitlist;
Fileinfo FileInfo;
extern int SubStringV3(const char * OriString,const char * StartToken, const char * EndToken, const int MaxNewStringfLenth,char NewString[]);
extern int my_strchr(char str [ ], char str1 [ ], char ch);
extern int getmark(char buf [ ], char ch);
extern int ConfigGetKey(char *CFG_file, void *section, void *key, char buf[]);
extern int ConfigSetKey(char * CFG_file, void* section, void * key, void * buf);
extern void getpath(char buff [ ], int maxnum, char pathname [ ]);
extern void strTrim(char buffer [ ]);
extern void LCDFileDir(char * filename);
extern char *getcwd(char *buf,size_t size);
void ReadConfigFile()
{
memset(&Conf,0,sizeof(Conf));
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Config","VerifyID",Conf.VerifyID);
//printf("%s\n",Conf.VerifyID);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Config","DataType",Conf.DataType);
//printf("%s\n",Conf.DataType);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Config","NodeID",Conf.NodeID);
//printf("%s\n",Conf.NodeID);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Config","CommServer",Conf.CommServer);
//printf("%s\n",Conf.CommServer);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Config","CommPort",Conf.CommPort);
ServPort2=BsAtoU(Conf.CommPort);
//printf("%s\n",Conf.CommPort);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Config","CommSpeed",Conf.CommSpeed);
VisitWsIntervalSeconds=BsAtoU(Conf.CommSpeed);
//printf("%d\n",VisitWsIntervalSeconds);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Config","ShutDown",Conf.ShutDown);
//printf("%s\n",Conf.ShutDown);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Config","FtpHost",Conf.FtpHost);
//printf("%s\n",Conf.FtpHost);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Config","FtpUser",Conf.FtpUser);
//printf("%s\n",Conf.FtpUser);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Config","FtpPass",Conf.FtpPass);
//printf("%s\n",Conf.FtpPass);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Status","ExsitPL",Conf.Uppl);
//printf("%s\n",Conf.Uppl);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Status","ExsitML",Conf.Upml);
//printf("%s\n",Conf.Upml);
ConfigGetKey(TmlConfigP_LocalConfigFileName,"Status","TpNumber",FileInfo.tempbuf1);//a模板编号
//printf("%s\n",FileInfo.tempbuf1);
}
void WriteConfigFile()
{
ConfigSetKey(TmlConfigP_LocalConfigFileName,"Status","ExsitPL",Conf.Uppl);
//printf("%s\n",Conf.Uppl);
ConfigSetKey(TmlConfigP_LocalConfigFileName,"Status","ExsitML",Conf.Upml);
//printf("%s\n",Conf.Upml);
ConfigSetKey(TmlConfigP_LocalConfigFileName,"Status","PlayList",FileInfo.PLNumber);
//ConfigSetKey(FileName,"Config","PlayFile",DownLoadingName);
ConfigSetKey(TmlConfigP_LocalConfigFileName,"Status","MsgList",FileInfo.tempbuf3);
ConfigSetKey(TmlConfigP_LocalConfigFileName,"Status","TpNumber",FileInfo.tempbuf1);
}
char* host2ip(char *host)
{
char *ptr,**pptr;
struct hostent *hptr;
char str[32];
char *address;
ptr = host;
address=malloc(20);
/*call gethostbyname(),get hostent struct*/
/*struct hostent{
char *h_name; //主机名
char **h_alias; //主机别名列表
int h_addrtype; //主机地址类型
int h_length; //主机地址长度
char **h_addr_list;//主机IP地址列表
}*/
if( (hptr = gethostbyname(ptr) ) == NULL )
{
// printf("gethostbyname error for host:%s\n", ptr);
return (char*)NULL;
}
for(pptr = hptr->h_aliases; *pptr != NULL; pptr++) ;
switch(hptr->h_addrtype)
{
case AF_INET:
pptr=hptr->h_addr_list;
for(;*pptr!=NULL;pptr++)
sprintf(address,"%s", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
break;
default:
printf("unknown address type\n");
break;
}
return address;
}
void Pack_Req_Data(char SendMessage[])
{
// printf("%s\n",Vplnumber.DnLoadingName);
char Playinfo[128];
char Mbytessize[10];
float Mbytes;
char sndmsg[]="GET /tangjun/commheart.aspx?";
Playinfo[0]='\0';
Mbytes=((float)TotalSize)/(1024*1024);
// printf("%f\n",Mbytes);
//printf("percent=%s\n",percent);
// printf("Flag=%d\n",Vplnumber.DnCompleteFlag);
sprintf(Mbytessize,"%5.1fM",Mbytes);
strTrim(Mbytessize);
// printf("Mbytessize=%s\n",Mbytessize);
memset(Appath,0,sizeof(Appath));
printf("%s\n",Vplnumber.DnLoadingName);
if(strlen(Vplnumber.DnLoadingName)!=0&&Vplnumber.DnCompleteFlag!=1)
{
strcat(DnLoadingname,Vplnumber.DnLoadingName);
strcat(DnLoadingname,"_");
strcat(DnLoadingname,Mbytessize);
strcat(DnLoadingname,"_");
strcat(DnLoadingname,percent);
}
else if(Vplnumber.DnCompleteFlag==1)
{
strcat(DnLoadingname,Vplnumber.DnLoadingName);
strcat(DnLoadingname,"100%");
}
else{
strcat(DnLoadingname,"");
}
// printf("%s\n",DnLoadingname);
strcat(Playinfo,FileInfo.PLNumber);
strcat(Playinfo,"^");
strcat(Playinfo,Vplnumber.DnLoadingName);
strcat(Playinfo,"^");
strcat(Playinfo,FileInfo.tempbuf3);
strcat(Playinfo,"^");
strcat(Playinfo,DnLoadingname);
//printf("%s\n",Playinfo);
strcat(Appath,"DataType=");
strcat(Appath,Conf.DataType);
strcat(Appath,"&Values=");
strcat(Appath,Conf.NodeID);
strcat(Appath,"|");
strcat(Appath,"|");
strcat(Appath,"|");
strcat(Appath,Playinfo);
strcat(Appath,"|");
strcat(Appath,Conf.Uppl);
strcat(Appath,"|");
strcat(Appath,Conf.Upml);
strcat(SendMessage,sndmsg);
strcat(SendMessage,Appath);
strcat(SendMessage," HTTP/1.1\r\n"
"host:127.0.0.1\r\n"
"Accept-Language:utf-8\r\n"
"Content-Type:text/html\r\n"
"user-Agent:Mozilla/4.0(compatible;MSIE 6.0)\r\n"
"accept:*/*\r\n\r\n");
}
int http_socket_comm(char tempchar[],char tempchar1[],char SndMsg[],char Servip[],int servport)
{
FILE *fpc=NULL;
int fd;
int sock;
int bytesSent,ToltalBytesSent,MsgSentLen; /* Length of string to be sent */
int bytesRcvd,ToltalHeadBytesRcvd,ToltalBodyBytesRcvd, AllTheContentLength ;
char * TokenContentLength = "Content-Length:";
char ValueContentLength[128];
memset(tempchar,0,sizeof(tempchar));
memset(tempchar1,0,sizeof(tempchar1));
if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) return Error_SocketCreate;
/* Construct the server address structure */
struct sockaddr_in ServAddr; /* Echo server address */
memset(&ServAddr, 0, sizeof(ServAddr)); /* Zero out structure */
ServAddr.sin_family = AF_INET; /* Internet address family */
ServAddr.sin_addr.s_addr = inet_addr(Servip); /* Server IP address */
ServAddr.sin_port = htons(servport); /* Server port */
// printf("please check internet status.....\n");
/* Establish the connection to the echo server */
if (connect(sock, (struct sockaddr *) &ServAddr, sizeof(ServAddr)) < 0)
return Error_SocketConnect;
//printf("connect success!\n");
//printf("Connect Server Success!Getting Heart Data......\n");
MsgSentLen = strlen(SndMsg); /* Determine input length */
/* Send the string to the server */
ToltalBytesSent = 0;
bytesSent=0;
do{
bytesSent = send(sock, SndMsg+ ToltalBytesSent, MsgSentLen - ToltalBytesSent , 0);
if ( bytesSent <= 0 ) return Error_SocketSendMsgLength;
ToltalBytesSent += bytesSent;
// printf("send success!\n");
}while(ToltalBytesSent < MsgSentLen);
/* Receive the same string back from the server */
ToltalHeadBytesRcvd=0;
bytesRcvd=0;
do {
bytesRcvd=recv(sock,tempchar+ToltalHeadBytesRcvd,1,0);
// bytesRcvd = recv(sock, ReceiveBuffer + TotalBytesRcvd , 1 , 0);
if (bytesRcvd<= 0 ) break;
ToltalHeadBytesRcvd= ToltalHeadBytesRcvd+ bytesRcvd; /* Keep tally of total bytes */
if(ToltalHeadBytesRcvd<4) continue;
if(tempchar[ToltalHeadBytesRcvd-4]=='\r'
&&tempchar[ToltalHeadBytesRcvd-3]=='\n'
&&tempchar[ToltalHeadBytesRcvd-2]=='\r'
&&tempchar[ToltalHeadBytesRcvd-1]=='\n')
{
SubStringV3(tempchar, TokenContentLength,"\r\n",128,ValueContentLength);
AllTheContentLength =BsAtoU(ValueContentLength);
//printf("AllTheContentLength=%d\n", AllTheContentLength );
ToltalBodyBytesRcvd=0;
//*tempchar1='\0';
do{
bytesRcvd=recv(sock,tempchar1+ToltalBodyBytesRcvd,BLOCKSIZE,0);
if(bytesRcvd<=0) break;
ToltalBodyBytesRcvd=ToltalBodyBytesRcvd+bytesRcvd;
if(MaxReceiveBufSize<ToltalBodyBytesRcvd+BLOCKSIZE+1) break;
if(ToltalBodyBytesRcvd>= AllTheContentLength ) break;
}while(bytesRcvd>0);
tempchar1[ToltalBodyBytesRcvd]='\0';
break;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -