#include "stdio.h" #include "conio.h" #include "stdlib.h" #include "string.h" #include "windows.h"
int times=0;// int haha(); int minus(); int check(); //////////////////////////////////////////////////////////////////////////////////////////////////// typedef int (*pDis)(); //同理 以后 pDis就可以代表这类的函数指针了 //int libfun(pDis pd);//函数指针做参数的libfun函数声明
static int soc_cb_http(int s, int e); /* 定义函数指针 */ static int http_cb(int s, int k, int p1, int p2); typedef int (*ms_http_cb_t)(int s, int e, int p1, int p2);
/* http 回调句柄 */ ms_http_cb_t cb=NULL; //实例化一个函指
// pDis cb; // pDis cb2;
int main(void)//相当于底层内核不断刷新等待消息来临 { pDis pd=NULL;//实例化一个函数指针 pd=soc_cb_http;//指向一个函数 cb=http_cb;
printf("*******************************************\n"); printf("* CALL_BACK_TEST *\n"); printf("*******************************************\n");
printf("begin! time is %d\n",times); while(1) { if(times<14) { pd(0,times); Sleep(3000); printf("/////////////////////////////////////////\n"); } else break; times++; } printf("last! time is %d\n",times); return 0; }
int check() { printf("just test!!!!!!!!!!!!!!!!!!!!!!!\n"); return 0; }
static int soc_cb_http(int s, int e) { int ret = 0; switch (e) { case 1: printf("1\n"); cb(s, 1, 3, 4); break; case 2: cb(s, 2, 0, 0); printf("2\n"); break; case 3: cb(s, 3, 0, 0); printf("3\n"); break; case 4: cb(s, 4, 0, 0); printf("4\n"); break; case 5: cb(1, 5, 3, 4); break; } return ret; }
static int http_cb(int s, int k, int p1, int p2) { static int sent = 0; printf("http_cb(%d, %d, %d, %d)", s, k, p1, p2); switch (k) { case 1: printf("正在查询DNS信息"); check();//调用简单函数看看 printf("k=%d\n",k); break; case 2: printf("查询DNS信息成功\n"); printf("k =%d\n",k); break; case 3: printf("查询DNS信息失败"); printf("DNS query failed!!!\n"); break; case 4: printf("正在连接"); printf("connectting...\n"); break; case 5: printf("已连接"); printf("Connected...\n"); break; case 6: printf("连接失败"); printf("Connect failed!!!\n"); break; case 7: printf("正在发送请求头"); printf("Sending request headers...\n"); break; case 8: printf("正在发送请求体"); printf("Sending request body..."); //这里准备调用其他函数 /* { int ret; char buf[8]; sprintf(buf, "12345678"); while ((ret = ms_http_post_body(s, buf, sizeof(buf))) > 0) { sent += ret; if (sent >= BODY_LEN) { //ms_http_post_body(s, "\r\n", 2); return sent; } }
if (ret == 0) { return sent; } else { return ret; } } */ break; case 9: printf("请求体发送完成"); printf("All request data sent...\n"); break; case 10: printf("正在接收响应码"); printf("Recieving response status-line, res code\n"); break; case 11: printf("正在接收响应头"); printf("Recieving response headers...\n"); /* { char val[20]; int val_len = sizeof(val); val_len = ms_http_get_header((char*)p1, "Content-Length", val, &val_len); printf("Response: Content-Length:%d, %d", atoi(val), val_len); } */ break; case 12: printf("正在接收响应体"); printf("Recieving response body...\n"); /* { int recv = 0; char * p = (char*)p1; while (recv < p2) { char buf[128] = {0}; int to_recv = sizeof(buf)-1<p2-recv?sizeof(buf)-1:p2-recv; memcpy(buf, p, to_recv); buf[to_recv] = 0; p += to_recv; recv += to_recv; printf("buf:%s", buf); } } */ break; case 13: printf("响应体接收完成"); printf("All response data recieved...\n"); break; } return 0; }
|