Code:#include <iostream> #include <windows.h> #include <stdio.h> #include <process.h> #include <fstream> #include <time.h> #include <tchar.h> #include "crtdbg.h" #include<direct.h> #include <string.h> #define FILENAME "C:\\start.txt" #define FILENAME3 "C:\\jacker.html" #define FILENAME2 "C:\\New Text Document.txt" #define url "localhost/jacker.html" #pragma comment(lib, "ws2_32.lib") using namespace std; unsigned Counter = 0; unsigned Counter2 = 0; unsigned __stdcall SecondThreadFunc( void* pArguments) { while(1) { time_t ltime; char tmpbuf[128]; Sleep(1000); //每隔1秒?行一次累加! /per sc Counter ++; //printf( "Counter is-> %d\n", Counter ); //test time counter _strtime( tmpbuf ); //printf( "OS time:%s\n", tmpbuf ); fstream _file; _file.open(FILENAME,ios::in); if(_file) { cout<<tmpbuf<<" - Transaction Detected\n"; //建立一个文件,然后读取后换成字符串,然后装在一个地方 /* ****************************************************** ***********当检测到有文件 开始 执行在网址抓取内容********** ******************************************************* */ WSADATA WSAData={0}; SOCKET sockfd; struct sockaddr_in addr; struct hostent *pURL; char myurl[BUFSIZ]; char *pHost = 0, *pGET = 0; char host[BUFSIZ], GET[BUFSIZ]; char header[BUFSIZ] = ""; static char text[BUFSIZ]; int i; /* * windows下使用socket必须用WSAStartup初始化,否则不能调用 */ if(WSAStartup(MAKEWORD(2,2), &WSAData)) { printf("WSA failed\n"); //return; } /* * 分离url中的主机地址和相对路径 */ strcpy(myurl, url); for (pHost = myurl; *pHost != '/' && *pHost != '\0'; ++pHost); if ( (int)(pHost - myurl) == strlen(myurl) ) strcpy(GET, "/"); else strcpy(GET, pHost); *pHost = '\0'; strcpy(host, myurl); //测试url是否一致 //printf("%s\n%s\n", host, GET); /* * 设定socket参数,未真正初始化 */ sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); pURL = gethostbyname(host); addr.sin_family = AF_INET; addr.sin_addr.s_addr = *((unsigned long*)pURL->h_addr); addr.sin_port = htons(80); /* * 组织发送到web服务器的信息 * 发送下面的信息HTTP协议的约定 */ //函数原型strcat(char[],const char[]); strcat(header, "GET "); strcat(header, GET); strcat(header, " HTTP/1.1\r\n"); strcat(header, "HOST: "); strcat(header, host); strcat(header, "\r\nConnection: Close\r\n\r\n"); /* * 连接到服务器,发送请求header,并接受反馈(网页源代码) */ connect(sockfd,(SOCKADDR *)&addr,sizeof(addr)); send(sockfd, header, strlen(header), 0); char htmlBuf[10000] = ""; //暂存区 while ( recv(sockfd, text, BUFSIZ, 0) > 0) { //printf("%s\n", text); strcat(htmlBuf, text); //把接收到的字符存放到htmlBuf //printf("anser\n",text); strnset(text, '\0', BUFSIZ); } //输出处理 //使用第一个 \r\n\r\n 作分界 char *pf = htmlBuf; while (*pf != '\0') { if (*pf == '\r' && *(pf+1) == '\n' && *(pf+2) == '\r' && *(pf+3) == '\n') //查找分界 break; pf++; } /* * *at here i printf("%s",pf+4); is ok can show the answer be can't user if decision Y/X * */ //FILE* pFile = NULL; //pFile = fopen(("%s",pf+4),"r"); //_ASSERT(pFile != NULL); char str[] = "X"; //fread(str,1,sizeof(str),pFile); if(strcmp(str,"Y")==0) { cout<<"yes"; printf("%s",pf+4); } else { cout<<"no"; printf("%s",pf+4); } //printf("%s",pf+4);//+4要的内容在分界后面,要跳过分界 } else { cout<<"Nnn\n"; } //fclose(pFile); } } int main() { HANDLE hThread; //HANDLE cCheck; unsigned threadID; //unsigned cCheck; printf( "Creating second thread...\n" ); // 做多一个line hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID ); //防止主程序退出... WaitForSingleObject( hThread, INFINITE); CloseHandle( hThread ); }