pre-emptive multithreading web spider


pre-emptive multithreading web spider

this article was contributed by sim ayers.

the win32 api supports applications that are pre-emptively multithreaded. this is a very useful and powerful feature of win32 in writing mfc internet spiders. the spider project is an example of how to use preemptive multithreading to gather information on the web using a spider/robot with the mfc wininet classes.

this project produces a spidering software program that checks web sites for broken url links. link verification is done only on href links. it displays a continously updated list of urls in a clistview that reports the status of the href link. the project could be used as a template for gathering and indexing information to be stored in a database file for queries.

search engines gather information on the web using programs called robots. robots (also called web crawlers, spiders, worms, web wanderers, and scooters) automatically gather and index information from around the web, and then put that information into databases. (note that a robot will index a page, and then follow the links on that page as a source for new urls to index.) users can than construct queries to search these databases to find the information they want.

by using preemptive multithreading, you can index a web page of url links, start a new thread to follow each new url link for a new source of urls to index.

the project uses the mdi cdocument used with a custom mdi child frame to display a ceditview when downloading web pages and a clistview when checking url links. the project also uses the cobarray, cinternetsession, chttpconnection, chttpfile, and cwinthread mfc classes. the cwinthread class is used to produce multiple threads instead of using the asynchronous mode in cinternetsession, which is realy left over from the winsock 16 bit windows platform.

the spider project uses simple worker threads to check url links or download a web page. the cspiderthread class is derived from the cwinthread class so each cspiderthread object can use the cwinthread message_map() function. by declaring a "declare_message_map()" in the cspiderthread class the user interface is still responsive to user input. this means you can check the url links on one web server and at the same time download and open a web page from another web server. the only time the user interface will become unresponsive to user input is when the thread count exceedes maximum_wait_objects which is defined as 64.

in the constructor for each new cspiderthread object we supply the threadproc function and the thread paramters to be passed to the threadproc function.

	cspiderthread* pthread;
	pthread = null;
	pthread = new cspiderthread(cspiderthread::threadfunc,pthreadparams); // create a new cspiderthread object

in the cspiderthread constructor we set the cwinthread* m_pthread pointer in the thread paramters structure so we can point to the correct instance of this thread;

pthreadparams->m_pthread = this;

the cspiderthread threadproc function

// simple worker thread proc function
uint cspiderthread::threadfunc(lpvoid pparam)
	threadparams * lpthreadparams = (threadparams*) pparam;
	cspiderthread* lpthread = (cspiderthread*) lpthreadparams->m_pthread;

	// use  sendmessage instead of postmessage here to keep the current thread count
	// synchronizied. if the number of threads is greater than maximum_wait_objects (64)
	// the program will be come	 unresponsive to user input

		wm_user_thread_done, 0, (lparam)lpthreadparams);  // deletes lpthreadparams and decrements the thread count

	return 0;

the structure passed to the cspiderthread threadproc function

typedef struct tagthreadparams
	hwnd m_hwndnotifyprogress;
	hwnd m_hwndnotifyview;
	cwinthread* m_pthread;
	cstring m_pszurl;
	cstring m_contents;
	cstring m_strservername;
	cstring m_strobject;
	cstring m_checkurlname;
	cstring m_string;
	dword m_dwservicetype;
	dword  m_threadid;
	dword m_status;
	urlstatus m_pstatus;
	internet_port  m_nport;
	int m_type;
	bool m_rootlinks;


after the cspiderthread object has been created we use the creatthread function to start the execution of the new thread object.

	if (!pthread->createthread())   //  starts execution of a cwinthread object
		afxmessagebox("cannot start new thread");
		delete pthread;
		pthread = null;
		delete pthreadparams;
		return false;

once the new thread is running we use the ::sendmessage function to send messages to the cdocument's-> clistview with the status structure of the url link.

	if(pthreadparams->m_hwndnotifyview != null)
		::sendmessage(pthreadparams->m_hwndnotifyview,wm_user_check_done, 0, (lparam) &pthreadparams->m_pstatus);

sturcture used for url status.

typedef struct tagurlstatus
	cstring m_url;
	cstring m_urlpage;
	cstring m_statusstring;
	cstring m_lastmodified;
	cstring m_contenttype;
	cstring m_contentlength;
	dword	m_status;
}urlstatus, * purlstatus;

each new thread creats a new cmyinternetsession (derived from cinternetsession) object with enablestatuscallback set to true, so we can check the status on all internetsession callbacks. the dwcontext id for callbacks is set to the thread id.

bool cinetthread::initserver()
		m_psession = new cmyinternetsession(agentname,m_nthreadid);
		int ntimeout = 30;  // very important, can cause a server time-out if set to low
							// or hang the thread if set to high.
		the time-out value in milliseconds to use for internet connection requests. 
		if a connection request takes longer than this timeout, the request is canceled.
		the default timeout is infinite. */
		m_psession->setoption(internet_option_connect_timeout,1000* ntimeout);
		/* the delay value in milliseconds to wait between connection retries.*/
		/* the retry count to use for internet connection requests. if a connection 
		attempt still fails after the specified number of tries, the request is canceled.
		the default is five. */

	catch (cinternetexception* pex)
		// catch errors from wininet
		m_psession = null;
		return false ;

	return true;

the key to using the mfc wininet classes in a single or multithread program is to use a try and catch block statement surrounding all mfc wininet class functions. the internet is very unstable at times or the web page you are requesting no longer exist, which is guaranteed to throw a cinternetexception error.

		// some mfc wininet class function
	catch (cinternetexception* pex)
		// catch errors from wininet
		return false ;

the maximum count of threads is initially set to 64, but you can configure it to any number between 1 and 100. a number that is too high will result in failed connections, which means you will have to recheck the url links.

a rapid fire succession of http requests in a /cgi-bin/ directory could bring a server to it's knees. the spider program sends out about 4 http request a second. 4 * 60 = 240 a minute. this can also bring a server to it's knees. be carefull about what server you are checking. each server has a server log with the requesting agent's ip address that requested the web file. you might get some nasty email from a angry web server administrator.

you can prevent any directory from being indexed by creating a robots.txt file for that directory. this mechanism is usually used to protect /cgi-bin/ directories. cgi scripts take more server resources to retrieve.

when the spider program checks url links it's goal is to not request too many documents too quickly. the spider program adheres somewhat to the standard for robot exclusion. this standard is a joint agreement between robot developers, that allows www sites to limit what url's the robot requests. by using the standard to limit access, the robot will not retrieve any documents that web server's wish to disallow.

before checking the root url, the program checks to see if there is a robots.txt file in the main directory. if the spider program finds a robots.txt file the program will abort the search. the program also checks for the meta tag in all web pages. if it finds a meta name="robots" content ="noindex,nofollow" tag it will not index the urls on that page.

windows 95
mfc/vc++ 5.0
wininet.h dated 9/25/97
wininet.lib dated 9/16/97
wininet.dll dated 9/18/97

can't seem to keep the thread count below 64 at all times.
limit of 32,767 url links in the clistview
wouldn't parse all urls correctly, will crash program occasionally using cstring functions with complex urls.

internet tools - fred forester
multithreading applications in win32
win32 multithreaded programming

download source code and example (65 kb)

last updated: 21 june 1998

  • 0
  • 0
  • 0
  • 一键三连
  • 扫一扫,分享海报

<p style="color:#666666;"> <span style="font-size:14px;">本门课程重实战,将基础知识拆解到项目里,让你在项目情境里学知识。</span> </p> <p style="color:#666666;"> <span style="font-size:14px;">这样的学习方式能让你保持兴趣、充满动力,时刻知道学的东西能用在哪、能怎么用。</span> </p> <p style="color:#666666;"> <span style="font-size:14px;">平时不明白的知识点,放在项目里去理解就恍然大悟了。</span> </p> <p style="color:#666666;"> <span></span> </p> <p style="color:#666666;"> <span style="font-size:14px;"> </span> </p> <p style="color:#666666;"> <span style="color:#FF0000;font-size:14px;"><strong>一、融汇贯通</strong></span> </p> <p style="color:#666666;"> <span style="font-size:14px;">本视频采用了前后端分离的开发模式,前端使用Vue.js+Element UI实现了Web页面的呈现,后端使用Python 的Django框架实现了数据访问的接口,前端通过Axios访问后端接口获得数据。在学习完本章节后,真正理解前后端的各自承担的工作。</span> </p> <p style="color:#666666;"> <span style="font-size:14px;"> </span> </p> <p style="color:#666666;"> <span style="color:#FF0000;font-size:14px;"><strong>二、贴近实战</strong></span> </p> <p style="color:#666666;"> <span style="font-size:14px;">本系列课程为练手项目实战:学生管理系统v4.0的开发,项目包含了如下几个内容:项目的总体介绍、基本功能的演示、Vuejs的初始化、Element UI的使用、在Django中实现针对数据的增删改查的接口、在Vuejs中实现前端增删改查的调用、实现文件的上传、实现表格的分页、实现导出数据到Excel、实现通过Excel导入数据、实现针对表格的批量化操作等等,所有的功能都通过演示完成、贴近了实战</span> </p> <p style="color:#666666;"> <span style="font-size:14px;"> </span> </p> <p style="color:#666666;"> <span style="color:#FF0000;font-size:14px;"><strong>三、课程亮点</strong></span> </p> <p style="color:#666666;"> <span style="font-size:14px;">在本案例中,最大的亮点在于前后端做了分离,真正理解前后端的各自承担的工作。前端如何和后端交互</span> </p> <p style="color:#666666;"> <span style="font-size:14px;"> </span> </p> <p style="color:#666666;"> <span style="color:#FF0000;font-size:14px;"><strong>适合人群:</strong></span> </p> <p style="color:#666666;"> <span style="font-size:14px;">1、有Python语言基础、web前端基础,想要深入学习Python Web框架的朋友;</span> </p> <p style="color:#666666;"> <span style="font-size:14px;">2、有Django基础,但是想学习企业级项目实战的朋友;</span> </p> <p style="color:#666666;"> <span style="font-size:14px;">3、有MySQL数据库基础的朋友</span> </p> <p style="color:#666666;"> <span style="font-size:14px;"> </span> </p> <p style="color:#666666;"> <span style="font-size:14px;"><img alt="" src="" /><br /> </span> </p> <p style="color:#666666;"> <span style="font-size:14px;"><br /> </span> </p>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
钱包余额 0