欢迎来到皮皮网官网

【灵魂碰撞系统源码】【源码配送小程序】【三端直播源码】crc源码 csdn

时间:2024-12-23 13:20:03 来源:征途 源码

1.CRC原理简介
2.实现web服务器中的gzip
3.const unsigned char *p = (const unsigned char*)&res; crc8(crc, p[3]); crc8(crc, p[2]);

crc源码 csdn

CRC原理简介

       æœ€è¿‘刚好有时间,整理了一下关于CRC的资料,详细对比了下程序的实现过程和原理,当然,高手都是不在意的。

        本文主要介绍CRC的一些基础知识,个人收获是后面关于网上标准Demo程序的一些详细解析。见下一篇tl.h#include string.h#include sys/stat.h#include signal.h#include stdio.h#include string.h#include assert.h#include zlib.h#define OS_CODE 0x /*Unix OS_CODE*/#define DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION#define DEFAULT_WINDOWSIZE -#define DEFAULT_MEMLEVEL 9#define DEFAULT_BUFFERSIZE #define MAX #define METHOD //获取数据的方式,使用字符串的长度#define URI //表示URI的最大长度#define VERSION //HTTP版本好的最大长度#define TYPE //表示文件的类型的长度int res_socket;void app_exit();static const char gzip_header[] = { '', '', Z_DEFLATED, 0, 0, 0, 0, 0, 0, OS_CODE};void put_long (unsigned char *string, unsigned long x) { string[0] = (x 0xff); string[1] = ((x 8) 0xff) ; string[2] = ((x ) 0xff) ; string[3] = ((x ) 0xff);}/*@description:将数据使用gzip压缩后发给浏览器@parametersockd:套接字file: 要压缩的内容了*/int gzip_buffer (int sockd, char * msg, int len) { z_stream stream;//zlib使用的。 int ret,灵魂碰撞系统源码 flush; char in[MAX];//存放输入的数据 char send[MAX + ];//存放压缩过后的数据 unsigned have; memcpy (send, gzip_header, ); memset (in, 0, len); stream.zalloc = Z_NULL; stream.zfree = Z_NULL; stream.opaque = Z_NULL; stream.avail_in = 0; stream.next_in = Z_NULL; memcpy (in, msg, len); //压缩初始化。 int tmp_result = deflateInit2(stream,源码配送小程序 Z_DEFAULT_COMPRESSION,//压缩级别 Z_DEFLATED,//压缩方式 -MAX_WBITS, 8, Z_DEFAULT_STRATEGY); if (Z_OK != tmp_result) { printf("deflateInit error: %drn", tmp_result); return 0; } stream.avail_in = len; //要压缩数据的长度 stream.next_in = in; //要压缩数据的首地址 stream.avail_out = MAX; //可存放的最大输出结果的长多。就是三端直播源码压缩后数据的最大长度 stream.next_out = send + ; //存放压缩数据的开始位置,send前十个字节用来放头部 ret = deflate (stream,28开奖预测源码Z_FINISH); //压缩 assert (ret != Z_STREAM_ERROR); switch (ret) { case Z_NEED_DICT: ret = Z_DATA_ERROR; case Z_DATA_ERROR: case Z_MEM_ERROR: (void)inflateEnd (stream); return ret; } have = MAX - stream.avail_out; unsigned crc = crc(0L, in, len); char * tail = send + + have; put_long (tail, crc); put_long (tail + 4, len); write (sockd, send, have + ); deflateEnd (stream); return 1;}/* @description:开始服务端监听@parameterip:web服务器的地址port:web服务器的端口@result:成功返回创建socket套接字标识,错误返回-1*/int socket_listen( char *ip,直播网站源码费用 unsigned short int port){ int res_socket; //返回值 int res, on; struct sockaddr_in address; struct in_addr in_ip; res = res_socket = socket(AF_INET, SOCK_STREAM, 0); setsockopt(res_socket, SOL_SOCKET, SO_REUSEADDR, on, sizeof(on)); memset(address, 0, sizeof(address)); address.sin_family = AF_INET ; address.sin_port =htons(port); address.sin_addr.s_addr = htonl(INADDR_ANY); //inet_addr(".0.0.1"); res = bind( res_socket, (struct sockaddr *) address, sizeof( address ) ); if(res) { printf( "port is used , not to repeat bindn" ); exit(); }; res = listen(res_socket,5); if(res) { printf( "listen port is error ;n" ); exit( ); }; return res_socket ;}/*@description:向客户端发送网页头文件的信息@parameterconn_socket:套接字描述符。status:/rentiansheng"Reage blog/a"; res_socket = socket_listen( ".0.0.1", ) ; //当按ctrl+c结束程序时调用,使用app_exit函数处理退出过程 signal(SIGINT, app_exit); while(1){ conn_socket = accept( res_socket, (struct sockaddr * )client_addr, len ); printf("reagen"); tmp = read (conn_socket, buf, MAX-1); buf [MAX - 1] = 0; send_http_head(conn_socket); gzip_buffer (conn_socket, msg, strlen(msg)); close(conn_socket); } }void app_exit(){ //回复ctrl+c组合键的默认行为 signal (SIGINT, SIG_DFL); //关闭服务端链接、释放服务端ip和端口 close(res_socket); printf("n"); exit(0);}

const unsigned char *p = (const unsigned char*)&res; crc8(crc, p[3]); crc8(crc, p[2]);

       ç”±äºŽres定义为unsigned int,所以包含4个字节;而p是unsigned char的指针,指向的数据是1个字节。

       æ‰€ä»¥å¸¸è§„来说,假如res由byte3 byte2 byte1 byte0构成,那么p[0]=byte0,p[1]=byte1,p[2]=byte2,p[3]=byte4,也就是p[0]是最低字节,p[3]是最高字节。

       ä½†æ˜¯è¿˜è¦çœ‹ç³»ç»Ÿç¡¬ä»¶è¿žæŽ¥ï¼Œæ˜¯big endian还是 little endian,如果是 little endian那么就是常规情况,和上面一样。如果是big endian,那么数据存放是倒过来的,也就是p[0]是最高字节,p[3]是最低字节,全部情况是p[0]=byte3,p[1]=byte2,p[2]=byte1,p[3]=byte0。

copyright © 2016 powered by 皮皮网   sitemap