自动更新系统(三)

来源:互联网 发布:初高中网络培训机构 编辑:程序博客网 时间:2024/06/11 05:56

客户端的程序

这个程序的功能现在基本是可以实现了,但是不能实现MISRA C的检测,有很多的小红帽,贴出来,希望有知道怎么改的大神不吝赐教。
message.cpp的代码:
#include "message.h"/****************************************************************************不能用字符串函数,用4字节存长度时,里面会有0,而字符串函数会把它当作结束符*不用uchar,就算是负数(超过127),接收那边复原 ***************************************************************************/CHAR *PackMessage(  const MSGE *ptmessage ){  CHAR *pcmessage = NULL;  UINT32 ui32index = 0u, ui32length = 0u, ui32i = 0u;  ULONG_64 ui32temp = 0u;  /*文件名字长度占一个字节*/  ui32length = strlen(ptmessage->FileName) + strlen(ptmessage->FileContent);  ui32length += (2u * sizeof(UINT32)) + 1u;      pcmessage = (CHAR *)calloc(1u, ui32length + 4u);    /*消息头*/   ui32temp = htonl((ULONG_64)ui32length);   (void)memcpy_s(pcmessage, ui32length + 4u, &ui32temp, 4u);   /*文件名字字节数*/   ui32index += 4u;   pcmessage[ui32index] = (CHAR)strlen(ptmessage->FileName);   /*文件名*/   ui32index += 1u;   (void)memcpy_s(&pcmessage[ui32index], ui32length + 4u, ptmessage->FileName, strlen(ptmessage->FileName));   /*偏移量*/  ui32temp = htonl(ptmessage->OffSet);  ui32index += strlen(ptmessage->FileName);      memcpy(&pcmessage[ui32index], &ui32temp, 4u);  /*块大小*/  ui32temp = htonl(ptmessage->BlockLength);      ui32index += 4u;  memcpy(&pcmessage[ui32index], &ui32temp, 4u);    /*文件块内容*/  ui32index += 4u;  memcpy(&pcmessage[ui32index], ptmessage->FileContent, strlen(ptmessage->FileContent));   return (pcmessage);}/*可能还需要一个参数,关于传进数组的大小,限制数组是某种格式*/MSGE *UnpackMessage(const CHAR acmessage[]   ){MSGE *ptmessage = NULL;UINT32 ui32index = 0u, ui32length = 0u, ui32i = 0u;ptmessage = (MSGE *)calloc(1, sizeof(MSGE));memcpy((CHAR *)&ui32length, acmessage, 4u);ui32length =ntohl(ui32length);ui32index += 5;memcpy(ptmessage->FileName, &acmessage[ui32index], acmessage[4]);ui32index += acmessage[4];memcpy((CHAR *)&ptmessage->OffSet, &acmessage[ui32index], 4u);ptmessage->OffSet = ntohl(ptmessage->OffSet);ui32index += 4;memcpy((CHAR *)&ptmessage->BlockLength, &acmessage[ui32index], 4u);ptmessage->BlockLength = ntohl(ptmessage->BlockLength);ui32index += 4;//if(acmessage[ui32index] != 0)  注意二进制    memcpy(ptmessage->FileContent, &acmessage[ui32index], ui32length-acmessage[4] - 9);return (ptmessage);}INT32 TransmitFile(   SOCKET sock,    CHAR acfilename[],   UINT32 ui32filesize  ){FILE *fp = NULL;MSGE *ptmsg = NULL, *ptrevmsg = NULL;MSGE tmessage = {0};CHAR *pcsendData = NULL, acrevData[CONTENTMAX + 50] = {0};  //不能用PACKETMAX替换255,会出问题,至少为201+31+13CHAR acfiledct[100] = "C:\\";CHAR acfolderdct[100] = "C:\\";INT32 i32loading=0, i32temp = -1, i32sendret = 0;UINT32 ui32ret = 0u, ui32remain = 0u, ui32num = 0u;UINT32 ui32i = 0u, ui32j = 0u, ui32k = 0u, ui32h = 0u;ptmsg = &tmessage;strcpy(ptmsg->FileName, acfilename);/****************************************************************************路径与文件名分离***************************************************************************/ui32j = strlen(acfilename) - 1;while(acfilename[ui32j] != '\\'){ui32j--;}ui32h = strlen(acfolderdct);for(ui32k = ui32h; ui32k < ui32j + ui32h; ui32k++){acfolderdct[ui32k] = acfilename[ui32k-ui32h];}mkdir(acfolderdct);strcat(acfiledct, acfilename);fp = fopen(acfiledct, "wb");  //注意:要用二进制打开,不然有问题if (NULL == fp){(void)OSM_Printf("error\n");}ui32num = ui32filesize / CONTENTMAX;ui32remain = ui32filesize % CONTENTMAX;    #if 0ui32j = strlen(acfilename) - 1;while(acfilename[ui32j] != '\\'){ui32j--;}while(acfilename[ui32j] != '\0'){acfilename[ui32k] = acfilename[ui32j+1];ui32k++;ui32j++;}#endif(void)OSM_Printf("%s:\n下载倒计时..",acfilename);    while (1)    {if (ui32i!=ui32num){ptmsg->OffSet = ui32i * CONTENTMAX;ptmsg->BlockLength = CONTENTMAX;}else{    if (0u == ui32i){ptmsg->OffSet = 0u;}else {ptmsg->OffSet += CONTENTMAX;}ptmsg->BlockLength = ui32remain;}pcsendData = PackMessage(ptmsg);  //因为含有0,调试框会显示不完全i32sendret = send(sock, pcsendData, (pcsendData[4] + 13), 0);  //sendData[4]为文件名字长度if (i32sendret != ((INT32)pcsendData[4] + 13)){ (void)OSM_Printf("发送错误\n");}    (void)OSM_Free(pcsendData);          ui32ret = recv(sock,acrevData, strlen(ptmsg->FileName) + (INT32)ptmsg->BlockLength + 13, 0);  if (ui32ret != strlen(ptmsg->FileName) + ptmsg->BlockLength + 13u){(void)OSM_Printf("接收错误\n"); }if(ui32ret > 0){i32loading = (ui32num - ui32i) / 800;if(i32loading != i32temp){i32temp = (ui32num-ui32i) / 800;(void)OSM_Printf("%d..", i32loading);}acrevData[ui32ret] = '\0';            ptrevmsg = UnpackMessage(acrevData);//fseek(fp,ptrevmsg->OffSet,0);(void)fwrite(ptrevmsg->FileContent, ptrevmsg->BlockLength, 1u, fp);     if (ptrevmsg != NULL){(void)OSM_Free(ptrevmsg);}ui32ret = 0;}ui32i++;if ((ui32i == (ui32num + 1)) || ((ui32i == ui32num) && (0 == ui32remain ))){break;}     }(void)fclose(fp);(void)OSM_Printf("\n下载完\n");return (SUCCESS);}void DeleteFile(const CHAR acfilename[]   ){CHAR DeleteDir[100] = "C:\\";strcat(DeleteDir, acfilename);OSM_Printf("删除了%s\n",DeleteDir);remove(DeleteDir);}CHAR *PacksMessage(   const LNode *ptmessage  ){CHAR *pcmessage = NULL;UINT32 ui32index = 0u, ui32length = 0u, ui32temp = 0u, ui32i = 0u;/*文件名字长度占一个字节*/ui32length = strlen(ptmessage->FILE_NAME) + strlen(ptmessage->SHA1) + 2 * sizeof(UINT32);pcmessage = (CHAR *)calloc(1, ui32length + 4u); /*消息头 总长度*/ui32temp = htonl(ui32length);memcpy(pcmessage, (CHAR *)&ui32temp, 4u);/*文件名字字符串大小*/ui32temp = htonl(strlen(ptmessage->FILE_NAME));ui32index += 4;memcpy(&pcmessage[ui32index], (CHAR *)&ui32temp, 4u);/*文件名字*/ui32index += 4;memcpy(&pcmessage[ui32index], ptmessage->FILE_NAME, strlen(ptmessage->FILE_NAME));/*sha1*/ui32index += strlen(ptmessage->FILE_NAME);memcpy(&pcmessage[ui32index], ptmessage->SHA1, strlen(ptmessage->SHA1));/*文件大小*/ui32temp = htonl(ptmessage->FILESIZE);ui32index += strlen(ptmessage->SHA1);memcpy(&pcmessage[ui32index], (CHAR *)&ui32temp, 4u);  return (pcmessage);}/*可能还需要一个参数,关于传进数组的大小,限制数组是某种格式*/LNode *UnpacksMessage(  const CHAR acmessage[] ){LNode *ptmessage = NULL;UINT32 ui32index = 0u, ui32length = 0u, ui32i = 0u;UINT32 filename_size = 0u;ptmessage = (LNode *)calloc(1, sizeof(LNode));/*取消息头*/memcpy((CHAR *)&ui32length, acmessage, 4u);ui32length =ntohl(ui32length);/*取文件名字总大小*/ui32index += 4;memcpy((CHAR *)&filename_size, &acmessage[ui32index], 4u);filename_size = ntohl(filename_size);/*取文件名字*/ui32index += 4;memcpy(ptmessage->FILE_NAME, &acmessage[ui32index], filename_size);/*取sha1*/ui32index += filename_size;memcpy((CHAR *)&ptmessage->SHA1, &acmessage[ui32index], ui32length-filename_size-8);/*取文件大小*/ui32index += ui32length-filename_size-8;memcpy((CHAR *)&ptmessage->FILESIZE, &acmessage[ui32index], 4u);ptmessage->FILESIZE = ntohl(ptmessage->FILESIZE);return (ptmessage);}
SHA1的代码:
#include "list.h"INT32 mnuSearch_i32OSM = 0;void  sha1_init(SHA1_CONTEXT *hd   ){  hd->h0 = 0x67452301U;  hd->h1 = 0xefcdab89U;  hd->h2 = 0x98badcfeU;  hd->h3 = 0x10325476U;  hd->h4 = 0xc3d2e1f0U;  hd->nblocks = 0U;  hd->count = 0;  }  /**************** * Transform the message X which consists of 16 32-bit-words */  static void  transform(   SHA1_CONTEXT *hd,   const UINT8 *data  ){  UINT32 a,b,c,d,e,tm;  UINT32 x[16];  /* get values from the chaining vars */  a = hd->h0;  b = hd->h1;  c = hd->h2;  d = hd->h3;  e = hd->h4;  #ifdef BIG_ENDIAN_HOST  memcpy( x, data, 64 );  #else  {  INT32 i;  UINT8 *p2;  for(i=0, p2=(UINT8*)x; i < 16; i++, p2 += 4 )   {  p2[3] = *data++;  p2[2] = *data++;  p2[1] = *data++;  p2[0] = *data++;  }  }  #endif  R( a, b, c, d, e, F1, K1, x[ 0] );  R( e, a, b, c, d, F1, K1, x[ 1] );  R( d, e, a, b, c, F1, K1, x[ 2] );  R( c, d, e, a, b, F1, K1, x[ 3] );  R( b, c, d, e, a, F1, K1, x[ 4] );  R( a, b, c, d, e, F1, K1, x[ 5] );  R( e, a, b, c, d, F1, K1, x[ 6] );  R( d, e, a, b, c, F1, K1, x[ 7] );  R( c, d, e, a, b, F1, K1, x[ 8] );  R( b, c, d, e, a, F1, K1, x[ 9] );  R( a, b, c, d, e, F1, K1, x[10] );  R( e, a, b, c, d, F1, K1, x[11] );  R( d, e, a, b, c, F1, K1, x[12] );  R( c, d, e, a, b, F1, K1, x[13] );  R( b, c, d, e, a, F1, K1, x[14] );  R( a, b, c, d, e, F1, K1, x[15] );  R( e, a, b, c, d, F1, K1, M(16) );  R( d, e, a, b, c, F1, K1, M(17) );  R( c, d, e, a, b, F1, K1, M(18) );  R( b, c, d, e, a, F1, K1, M(19) );  R( a, b, c, d, e, F2, K2, M(20) );  R( e, a, b, c, d, F2, K2, M(21) );  R( d, e, a, b, c, F2, K2, M(22) );  R( c, d, e, a, b, F2, K2, M(23) );  R( b, c, d, e, a, F2, K2, M(24) );  R( a, b, c, d, e, F2, K2, M(25) );  R( e, a, b, c, d, F2, K2, M(26) );  R( d, e, a, b, c, F2, K2, M(27) );  R( c, d, e, a, b, F2, K2, M(28) );  R( b, c, d, e, a, F2, K2, M(29) );  R( a, b, c, d, e, F2, K2, M(30) );  R( e, a, b, c, d, F2, K2, M(31) );  R( d, e, a, b, c, F2, K2, M(32) );  R( c, d, e, a, b, F2, K2, M(33) );  R( b, c, d, e, a, F2, K2, M(34) );  R( a, b, c, d, e, F2, K2, M(35) );  R( e, a, b, c, d, F2, K2, M(36) );  R( d, e, a, b, c, F2, K2, M(37) );  R( c, d, e, a, b, F2, K2, M(38) );  R( b, c, d, e, a, F2, K2, M(39) );  R( a, b, c, d, e, F3, K3, M(40) );  R( e, a, b, c, d, F3, K3, M(41) );  R( d, e, a, b, c, F3, K3, M(42) );  R( c, d, e, a, b, F3, K3, M(43) );  R( b, c, d, e, a, F3, K3, M(44) );  R( a, b, c, d, e, F3, K3, M(45) );  R( e, a, b, c, d, F3, K3, M(46) );  R( d, e, a, b, c, F3, K3, M(47) );  R( c, d, e, a, b, F3, K3, M(48) );  R( b, c, d, e, a, F3, K3, M(49) );  R( a, b, c, d, e, F3, K3, M(50) );  R( e, a, b, c, d, F3, K3, M(51) );  R( d, e, a, b, c, F3, K3, M(52) );  R( c, d, e, a, b, F3, K3, M(53) );  R( b, c, d, e, a, F3, K3, M(54) );  R( a, b, c, d, e, F3, K3, M(55) );  R( e, a, b, c, d, F3, K3, M(56) );  R( d, e, a, b, c, F3, K3, M(57) );  R( c, d, e, a, b, F3, K3, M(58) );  R( b, c, d, e, a, F3, K3, M(59) );  R( a, b, c, d, e, F4, K4, M(60) );  R( e, a, b, c, d, F4, K4, M(61) );  R( d, e, a, b, c, F4, K4, M(62) );  R( c, d, e, a, b, F4, K4, M(63) );  R( b, c, d, e, a, F4, K4, M(64) );  R( a, b, c, d, e, F4, K4, M(65) );  R( e, a, b, c, d, F4, K4, M(66) );  R( d, e, a, b, c, F4, K4, M(67) );  R( c, d, e, a, b, F4, K4, M(68) );  R( b, c, d, e, a, F4, K4, M(69) );  R( a, b, c, d, e, F4, K4, M(70) );  R( e, a, b, c, d, F4, K4, M(71) );  R( d, e, a, b, c, F4, K4, M(72) );  R( c, d, e, a, b, F4, K4, M(73) );  R( b, c, d, e, a, F4, K4, M(74) );  R( a, b, c, d, e, F4, K4, M(75) );  R( e, a, b, c, d, F4, K4, M(76) );  R( d, e, a, b, c, F4, K4, M(77) );  R( c, d, e, a, b, F4, K4, M(78) );  R( b, c, d, e, a, F4, K4, M(79) );  /* Update chaining vars */  hd->h0 += a;  hd->h1 += b;  hd->h2 += c;  hd->h3 += d;  hd->h4 += e;  }  /* Update the message digest with the contents * of INBUF with length INLEN. */  static void  sha1_write(SHA1_CONTEXT *hd,const UINT8 *inbuf,UINT32 inlen   ){  if( hd->count == 64 )/* flush the buffer */  { transform( hd, hd->buf );  hd->count = 0;  hd->nblocks++;  }  if( !inbuf  )  return;  if( hd->count ) {  for( ; (inlen) && (hd->count < 64); inlen-- )  {hd->buf[hd->count++] = *inbuf++;  }sha1_write( hd, NULL, 0U );  if( !inlen )  return;  }  while( inlen >= 64U ) {  transform( hd, inbuf );  hd->count = 0;  hd->nblocks++;  inlen -= 64U;  inbuf += 64U;  }  for( ; (inlen) && (hd->count < 64); inlen-- )  {hd->buf[hd->count++] = *inbuf++;  }}  /* The routine final terminates the computation and * returns the digest. * The handle is prepared for a new cycle, but adding bytes to the * handle will the destroy the returned buffer. * Returns: 20 bytes representing the digest. */  static void  sha1_final(SHA1_CONTEXT *hd   ){  UINT32 t, msb, lsb;  UINT8 *p;  sha1_write(hd, NULL, 0U); /* flush */  t = hd->nblocks;  /* multiply by 64 to make a byte count */  lsb = t << 6;  msb = t >> 26;  /* add the count */  t = lsb;  if( (lsb += hd->count) < t )  {msb++;  }/* multiply by 8 to make a bit count */  t = lsb;  lsb <<= 3;  msb <<= 3;  msb |= t >> 29;  if( hd->count < 56 )   /* enough room */{   hd->buf[hd->count++] = 0x80U; /* pad */  while( hd->count < 56 ) {hd->buf[hd->count++] = 0U;  /* pad */  }}  else/* need one extra block */  { hd->buf[hd->count++] = 0x80U; /* pad character */  while( hd->count < 64 ) {hd->buf[hd->count++] = 0U;  }sha1_write(hd, NULL, 0U);  /* flush */;  memset(hd->buf, 0, 56U ); /* fill next block with zeroes */  }  /* append the 64 bit count */  hd->buf[56] = msb >> 24;  hd->buf[57] = msb >> 16;  hd->buf[58] = msb >>  8;  hd->buf[59] = msb       ;  hd->buf[60] = lsb >> 24;  hd->buf[61] = lsb >> 16;  hd->buf[62] = lsb >>  8;  hd->buf[63] = lsb       ;  transform( hd, hd->buf );  p = hd->buf;  #ifdef BIG_ENDIAN_HOST  #define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)  #else /* little endian */  #define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16; *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)  #endif  X(0);  X(1);  X(2);  X(3);  X(4);  #undef X  }  /*获取文件的SHA1值,如果发生错误则将错误信息写入outError * FileNameInPut:文件路径 * outSHA1:SHA1输出变量 * outError:错误信息输出变量 * returns:outSHA1 */  char *GetFileSHA1(  char *FileNameInPut,  char *outSHA1,  char *outError ){  if(FileNameInPut==NULL)   {  if (outError != NULL)  {  sprintf(outError, "%s", "FileNameInPut Is NULL");  }  }  else{FILE *fp;  CHAR buffer[4096];  UINT32 n;  SHA1_CONTEXT ctx;  INT32 i32i = 0;  CHAR p[100] = "C:\\server\\";strcat(p,FileNameInPut);fopen_s (&fp, p , "rb");  if (!fp)              {  if (outError != NULL)  {  /*sprintf(outError, "打开文件“%s”失败\n", FileNameInPut);  */}  }else{sha1_init (&ctx);  while ( (n = fread (buffer, 1, sizeof(buffer), fp)))     {sha1_write (&ctx, (UINT8 *)buffer, n);  }if (ferror (fp))  {  if (outError != NULL)  {  sprintf(outError, "读取文件“%s”失败\n", FileNameInPut);  }   }else{sha1_final (&ctx);  fclose (fp);  for ( i32i=0; i32i < 20; i32i++)  {  sprintf(outSHA1 + 2*i32i, "%02x", (UINT8)ctx.buf[i32i]);  }  outSHA1[2*i32i] = '\0';  }}}return outSHA1;  }  LNode *GenerateSHA1(void   ){CHAR *argv;CHAR *argv1;LNode *head;LONG_64 Handle;LONG_64 Handle1;struct _finddata_t FileInfo;struct _finddata_t FileInfo1;head = CreateList();Handle = _findfirst("C:\\server\\*.*", &FileInfo);if(Handle == -1L){mnuSearch_i32OSM = OSM_Printf("没有找到匹配的项目\n");}else{do{CHAR sha1[41] = { 0 };argv = FileInfo.name;if ((argv)[0] != '.'){if (_A_SUBDIR == FileInfo.attrib){CHAR t[100] = "C:\\server\\";CHAR q[100];strcpy(q, FileInfo.name);strcat(t,FileInfo.name);strcat(t,"\\*.*");Handle1 = _findfirst(t, &FileInfo1);if(Handle == -1L){mnuSearch_i32OSM = OSM_Printf("没有找到匹配的项目\n");}else{do{CHAR sha1[41] = { 0 };argv1 = FileInfo1.name;if ((argv1)[0] != '.'){//mnuSearch_i32OSM = OSM_Printf("\r\n");  CHAR eror[256] = { 0 };  CHAR pq[100] = "server\\";strcat(pq,q);strcat(pq,"\\");strcat(pq,FileInfo1.name);//mnuSearch_i32OSM = OSM_Printf("%s", pq);//mnuSearch_i32OSM = OSM_Printf("\t%d\r\n", FileInfo1.size);//mnuSearch_i32OSM = OSM_Printf("SHA1值为:");//mnuSearch_i32OSM = OSM_Printf("%s\r\n", GetFileSHA1(argv1, sha1, NULL));  if (strlen(eror) != 0U)  {  mnuSearch_i32OSM = OSM_Printf("获取SHA1发生错误:%s\r\n", eror);  }  AddElem(head, pq, GetFileSHA1(argv1, sha1, NULL), FileInfo1.size);}}while(_findnext(Handle1, &FileInfo1) == 0);}}else{//mnuSearch_i32OSM = OSM_Printf("\r\n");  CHAR eror[256] = { 0 };  CHAR p[100] = "server\\";strcat(p,FileInfo.name);//mnuSearch_i32OSM = OSM_Printf("%s", p);//mnuSearch_i32OSM = OSM_Printf("\t%d\r\n", FileInfo.size);//mnuSearch_i32OSM = OSM_Printf("SHA1值为:");//mnuSearch_i32OSM = OSM_Printf("%s\r\n", GetFileSHA1((argv), sha1, NULL));  if (strlen(eror) != 0U)  {  mnuSearch_i32OSM = OSM_Printf("获取SHA1发生错误:%s\r\n", eror);  }  AddElem(head, p, GetFileSHA1((argv), sha1, NULL), FileInfo.size);}}}while(_findnext(Handle, &FileInfo) == 0);}return (head); }
SHA1_List.cpp的源代码:
#include "list.h"LNode *CreateList(void){LNode *head;head = (LNode*)calloc(1,sizeof(LNode));/* 申请一个动态空间,作为头指针*/head->pNext = NULL;return (head);}void AddElem( LNode *pHead,  CHAR FILE_NAME[NAMELENGTH], CHAR SHA1_VALUE[NAMELENGTH], INT32 FILESIZE){DataType i32OSM = 0;LNode  *q, *r;DataType COUNT = 0;if (NULL == pHead){i32OSM = OSM_Printf("\t\t\t1.链表为空!!\n");}else{q = pHead;  /* 将头指针pHead给q*/while(NULL != q->pNext)  /* 当到达链表尾循环停止*/{q = q->pNext;}r = (LNode*)calloc(1,sizeof(LNode));strcpy(r->FILE_NAME, FILE_NAME);strcpy(r->SHA1, SHA1_VALUE);r->FILESIZE = FILESIZE;r->pNext = NULL;q->pNext = r;}}void DeleteSHA1(LNode *pHead, CHAR SHA1[100]/* 元素的值*/   ){LNode *p, *q;Status i32Ret;if (NULL == pHead){i32Ret = OPERATION_FAILED;/* 链表空返回LINKLIST_EMPTY = 3*/}else{q = pHead;/* 将头指针pHead给q*/p = pHead->pNext;/* 使p指向头指针的下一个节点*/if (NULL == p){i32Ret = POINTER_NULL;/* 指针为空返回POINTER_NULL = 1*/}else{while(p != NULL){if (0 == strcmp(p->SHA1, SHA1))/* 找到元素所在的节点*/{q->pNext = p->pNext;/* 使q的next指向p的next*/free(p);break;}else{q = q->pNext;/* 否则一直往下寻找*/p = p->pNext;}}i32Ret = OPERATION_SUCCED;/* 找到没找到都返回OPERATION_SUCCED = 6*/}}}
接下来的是ClientMain.cpp的程序:
#include "message.h"SOCKET sclient;SOCKADDR_IN serAddr;INT32 SHA1Length = 0;INT32 SeverRecDataLength = 0;LNode *C_head;LNode *UpdataHead = NULL, *DeleteHead = NULL;void CompareSha1();INT32 main(       INT32 argc,   const char*  argv[]  ){ /*   初始化WSA */INT32 OSM = 0;LNode *UpHead;LNode *LiuHead;WORD sockVersion = MAKEWORD(2, 2);WSADATA data; C_head = GenerateSHA1(); if( WSAStartup(sockVersion, &data) != 0)//sockVersion是一个无符号16位整形数{//WSAStartup异步套接字的启动命令return 0;}/* 创建套接字:AF_INET决定了要用ipv4地址(32位的)与端口号(16位的)的组合流式Socket(SOCK_STREAM)是一种面向连接的Socket,针对于面向连接的TCP服务应用 */sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);/*INVALID_SOCKET调用失败返回值*/if(sclient == INVALID_SOCKET){OSM = OSM_Printf("invalid socket !");return 0;}//连接IP和端口serAddr.sin_family = AF_INET;//使用IP地址家族serAddr.sin_port = htons(8888u);serAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); if (connect(sclient, (SOCKADDR *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR){OSM = OSM_Printf("没有连接成功 !");closesocket(sclient);}else{CompareSha1();LiuHead =UpdataHead;if(LiuHead->pNext == NULL){OSM = OSM_Printf("不需更新\n");}while(LiuHead->pNext != NULL){LiuHead =LiuHead->pNext;TransmitFile(sclient,LiuHead->FILE_NAME,LiuHead->FILESIZE);}LiuHead =DeleteHead;while(LiuHead->pNext != NULL){LiuHead =LiuHead->pNext;DeleteFile(LiuHead->FILE_NAME);}closesocket(sclient);}while(1);WSACleanup();return (0);}void CompareSha1( void){INT32 ret;LNode *revnode;LNode *ClientNode;LNode *ServerNode,*tempNode;INT32 i32i = 0;INT32 i32j = 0;INT32 i32k = 0;CHAR ClientFileSHA1Bag[255] = {NULL};CHAR SeverFileSHA1Bag[255] = {NULL};tempNode  = CreateList();UpdataHead = CreateList();DeleteHead = CreateList();while(1){CHAR recData[255] = {NULL};ret = recv(sclient, recData, 255, 0);if(strcmp(recData, "end") != 0){  revnode = UnpacksMessage(recData);AddElem(tempNode  , revnode->FILE_NAME, revnode->SHA1, revnode->FILESIZE);free(revnode);/*i32j = 0;*/}else{   break;}}ServerNode = tempNode;while (ServerNode->pNext != NULL){ServerNode = ServerNode->pNext;ClientNode = C_head;ClientNode =ClientNode->pNext ;while(ClientNode != NULL){if (0 == strcmp(ClientNode->FILE_NAME, ServerNode->FILE_NAME)){if (0 == strcmp(ClientNode->SHA1, ServerNode->SHA1)){;}else{AddElem(UpdataHead, ServerNode->FILE_NAME, ServerNode->SHA1,ServerNode->FILESIZE);}break;}ClientNode = ClientNode->pNext;}if (ClientNode == NULL){AddElem(UpdataHead, ServerNode->FILE_NAME, ServerNode->SHA1,ServerNode->FILESIZE);}}ClientNode = C_head;while (ClientNode->pNext != NULL){ClientNode = ClientNode->pNext;ServerNode = tempNode;ServerNode =ServerNode->pNext ;while(ServerNode != NULL){if (0 == strcmp(ClientNode->FILE_NAME, ServerNode->FILE_NAME)){if (0 == strcmp(ClientNode->SHA1, ServerNode->SHA1)){;}else{AddElem(DeleteHead, ClientNode->FILE_NAME, ClientNode->SHA1,ClientNode->FILESIZE);}break;}ServerNode =ServerNode->pNext;}if (ServerNode == NULL){AddElem(DeleteHead, ClientNode->FILE_NAME, ClientNode->SHA1,ClientNode->FILESIZE);}}if (C_head == NULL){UpdataHead = tempNode;}}






1 0
原创粉丝点击