1.CRCåçç®ä»
2.实现web服务器中的gzip
3.const unsigned char *p = (const unsigned char*)&res; crc8(crc, p[3]); crc8(crc, p[2]);
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,波场php源码 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,趋势划线选股源码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ã