找到二进制字符串中连续最长'0'出现位置
2011-08-23 00:08
239 查看
任务:
1.制作http服务器,读取url提交的相关数据.
2.把读到的数据推入到队列中.
条件:
使用libevent的类库,所以先安装libevent
1.服务端简易代码如下
编译:(编译时把libevent的类库中的.so文件和.h文件连接
进来)
测试
在服务器端,执行编译后的文件a.out
客户端进行访问(也可以使用浏览器访问,但是要打开端口号2345——vi /etc/sysconfig/iptables)
1.制作http服务器,读取url提交的相关数据.
2.把读到的数据推入到队列中.
条件:
使用libevent的类库,所以先安装libevent
tar zxvf libevent-2.0.12-stable.tar.gz cd libevent-2.0.12-stable/ ./configure --prefix=/usr/local/libevent-2.0.12-stable/ make make install cd ../
1.服务端简易代码如下
#include <stdio.h> #include <stdlib.h> #include <err.h> #include <event.h> #include <evhttp.h> void http_handle(struct evhttp_request *req, void *arg); /* HTTP Request Handle */ int main(){ struct evhttp *httpd; event_init(); httpd = evhttp_start("0.0.0.0", 2345); if (httpd == NULL) { fprintf(stderr, "Error: Unable to listen on %s:%d\n\n"); exit(1); } evhttp_set_timeout(httpd, 2000); evhttp_set_gencb(httpd, http_handle, NULL); event_dispatch(); evhttp_free(httpd); return 0; } void http_handle(struct evhttp_request *req, void *arg){ struct evbuffer *buf; buf = evbuffer_new(); /* Response the client */ evhttp_send_reply(req, HTTP_OK, "OK", buf); //evbuffer_add_printf(buf, "%s", "HTTPSQS_AUTH_FAILED"); /* Release the memory */ evbuffer_free(buf); fprintf(stderr,"Send \n"); }
编译:(编译时把libevent的类库中的.so文件和.h文件连接
进来)
gcc http.c -L/usr/local/libevent-2.0.12-stable/lib/ -levent -I/usr/local/libevent-2.0.12-stable/include/
测试
在服务器端,执行编译后的文件a.out
./a.out
客户端进行访问(也可以使用浏览器访问,但是要打开端口号2345——vi /etc/sysconfig/iptables)
[www@zhoubc data]$ curl http://127.0.0.1:2345[/code]
此时再看服务端,变成如下状态
[www@zhoubc queue]$ ./a.out Send
2.处理http请求,重写htt_handle方法
void http_handle(struct evhttp_request *req, void *arg){ struct evbuffer *buf; buf = evbuffer_new(); /* Analyst the URI */ char *decode_uri = strdup((char*) evhttp_request_uri(req)); struct evkeyvalq http_query; evhttp_parse_query(decode_uri, &http_query); free(decode_uri); /* URI Parameter */ const char *http_input_opt = evhttp_find_header (&http_query, "opt"); /* Operation Type */ const char *http_input_name = evhttp_find_header (&http_query, "name"); /* Queue Name */ const char *http_input_data = evhttp_find_header (&http_query, "data"); /* Data With GET */ /* header */ evhttp_add_header(req->output_headers, "Content-Type", "text/plain"); evhttp_add_header(req->output_headers, "Connection", "keep-alive"); evhttp_add_header(req->output_headers, "Cache-Control", "no-cache"); evhttp_add_header(req->output_headers, "author", "Dennis .Z Ritchie"); if(http_input_opt != NULL && http_input_name != NULL && strlen(http_input_name) < 300){ /* GET Method,OUT The Queue */ if(strcmp(http_input_opt,"put") == 0){ int buffer_data_len = EVBUFFER_LENGTH(req->input_buffer); if(buffer_data_len > 0){ /* POST METHOD */ char *input_value_data = EVBUFFER_DATA(req->input_buffer); /* Submited Data */ fprintf(stderr,"%s \n",input_value_data); }else if(http_input_data != NULL){ fprintf(stderr,"%s \n",http_input_data); } }else if(strcmp(http_input_opt,"get") == 0){ } } /* Response the client */ evhttp_send_reply(req, HTTP_OK, "OK", buf); //evbuffer_add_printf(buf, "%s", "HTTPSQS_AUTH_FAILED"); /* Release the memory */ evhttp_clear_headers(&http_query); evbuffer_free(buf); }
相关文章推荐
- 在一个字符串(1<=字符串长度<=10000,全部由大小写字母组成)中找到第一个只出现一次的字符,并返回它的位置
- 【字符串处理算法】最长连续字符及其出现次数的算法设计及C代码实现
- 微软面试100题---在字符串中找到连续最长的数字串
- 用三重循环求一个字符串的最大回文串(连续重复出现的最长子串)
- 字符串中连续出现最多的子串 & 字符串中最长反复子串
- 26.在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符,并返回它的位置
- 字符串中连续出现最多的子串 & 字符串中最长重复子串
- 统计一个二进制字符串连续出现0或者连续出现1的最大次数
- 输入一行字符串,找出其中出现的相同且长度最长的字符串,输出他及其首字符串位置
- 输入一行字符串,找出其中出现的相同且长度最长的字符串,输出它及其首字符的位置
- 26.在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符,并返回它的位置
- 字符串编程问题:求连续出现最长字符串问题(js实现)
- 求字符串中出现次数最多且最长的子串、连续出现次数最多的子串
- 2015输入一个字符串以#结尾,则输出此字符串中连续出现最长的数字串及其开始的下标;
- 找到B字符串在A字符串中出现的所有位置的集合
- 1.找到一个字符串各次出现的位置
- 编写一个函数 char *strfind(char *s, char *t),用于查找字符串t在字符串s中最右边出现的位置,如果没有找到则返回NULL.
- 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符,并返回它的位置
- 输入一行字符串,找出其中重复出现且最长的字符串,输出它及其首字符的位置
- 给一个字符串,例如”ababc",要求返回"ab"。因为"ab”连续重复出现且最长。用C/C++语言写一个函数完成该算法,给出复杂度