FTP Asynchronous Model test network State __FTP

Source: Internet
Author: User
Tags ftp connection

To open the FTP test thread:

1, the thread declaration:

Static DWORD WINAPI Ftpcheckthread (LPVOID lpparam);

2, Thread Open:

CloseHandle (CreateThread (null,0,ftpcheckthread,this,0,null));

3. Thread implementation:

InternetOpen the last parameter is set to the asynchronous model.

DWORD WINAPI Cmainframe::ftpcheckthread (lpvoid lpparam) {cmainframe* pThis = (cmainframe*) Lpparam;

	CString Strip,strport,struser,strpassword;

	BOOL bsuccess = FALSE;
	Hinternet hinternet = NULL;

	Hinternet hftp = NULL;

	netinfo* pnetinfo = new NETINFO;

		if (pnetinfo!= null) {pnetinfo->m_hevent = CreateEvent (null, TRUE,//manual FALSE,//Initial no signal NULL);

		pnetinfo->m_bsuccess = FALSE;
	Pnetinfo->m_hhandle = NULL;
	} pthis->m_dlgbar.getdlgitemtext (Idc_ed_ip,strip);
	Pthis->m_dlgbar.getdlgitemtext (Idc_ed_port,strport);
	Pthis->m_dlgbar.getdlgitemtext (Idc_ed_user,struser);

	Pthis->m_dlgbar.getdlgitemtext (Idc_ed_password,strpassword);
			while (TRUE) {if (hinternet!= NULL) {internetclosehandle (hinternet);
			Sleep (200);						
		Hinternet = NULL;

		} hinternet = InternetOpen (Null,internet_open_type_preconfig,null,null,internet_flag_async); if (hinternet!= NULL) {internetsetstatuscallback (Hinternet,internetstatuscallback)//Set callback function if (hftp!= NULL) {internetclosehandle (HFTP);
				Sleep (500);
			Hftp = NULL;
			} tchar* Pwidechar = Strport.getbuffer (Strport.getlength ());
			char* Pchar = Widechartoansi (Pwidechar);

			Strport.releasebuffer (); Hftp = InternetConnect (Hinternet,strip,atoi (pchar), Struser,strpassword,internet_service_ftp,internet_flag_

			Passive, (DWORD_PTR) pnetinfo); if (Hftp && GetLastError ()!= error_io_pending) {//pthis->messagebox (_t) ("The first connection succeeds.")
				"));
				Gstrnetworkmsg = _t ("Network connection Successful!");
				
			::P ostmessage (pthis->m_hwnd,wm_netstatemsg,0,0); }else{if (WaitForSingleObject (pnetinfo->m_hevent,10000) = = wait_object_0) {resetevent (pNetInfo->m_hEv

					ENT);
					Hftp = pnetinfo->m_hhandle;
				Pnetinfo->m_hhandle = NULL;
					} if (hftp = = NULL) {//network connection failed, attempting to reconnect gstrnetworkmsg = _t ("Network connection failed, trying to reconnect ...");
				::P ostmessage (pthis->m_hwnd,wm_netstatemsg,0,0); } else {//network connection succeeded ...。
					Gstrnetworkmsg = _t ("Network connection Successful!");

					::P ostmessage (pthis->m_hwnd,wm_netstatemsg,0,0);

					Sleep (1000);
					bsuccess = Ftpcommadcheck (Hftp,pnetinfo);
						if (bsuccess) {//network OK gstrnetworkmsg = _t ("Network ok!");
					::P ostmessage (pthis->m_hwnd,wm_netstatemsg,0,0);
						else {//network Error gstrnetworkmsg = _t ("Network error!");
					::P ostmessage (pthis->m_hwnd,wm_netstatemsg,0,0);
			} sleep (1000);
			} delete[] Pchar; //......
		}
		Else{Sleep (1000);
	Sleep (10000);
return 1; }

Where: Ftpcheckthread () functions involved in the implementation of several functions are as follows:

1 the Declaration and implementation of the callback function:

void CALLBACK Internetstatuscallback (
	hinternet hinternet,
	dword_ptr dwcontext,
	DWORD dwinternetstatus,
	lpvoid Lpvstatusinformation,
	DWORD dwstatusinformationlength
	);

void CALLBACK Internetstatuscallback (
	hinternet hinternet,
	dword_ptr dwcontext,
	DWORD dwinternetstatus,
	lpvoid Lpvstatusinformation,
	DWORD dwstatusinformationlength)
{
	if (dwinternetstatus = = Internet_status_request_complete)
	{
		Internet_async_result *result = (internet_async_result*) lpvstatusinformation;
		if (Result->dwerror = = ERROR_SUCCESS)
		{
			(netinfo*) dwcontext)->m_hhandle = (hinternet) result-> Dwresult;
			((netinfo*) dwcontext)->m_bsuccess = (BOOL) result->dwresult;
			SetEvent (((netinfo*) dwcontext)->m_hevent);}}

2) Implementation of Widechartoansi:

Char *widechartoansi (wchar_t *pwidechar)  
{  
	if (!pwidechar) return NULL;  
	char *pszbuf = NULL;  
	int needbytes = WideCharToMultiByte (CP_ACP, 0, Pwidechar,-1, NULL, 0, NULL, NULL);  
	if (Needbytes > 0) {  
		pszbuf = new char[needbytes+1];  
		ZeroMemory (Pszbuf, (needbytes+1) *sizeof (char));  
		WideCharToMultiByte (CP_ACP, 0, Pwidechar,-1, pszbuf, needbytes, NULL, NULL);  

	return pszbuf;  
}

3) The implementation of the Ftpcommadcheck function:

BOOL Ftpcommadcheck (hinternet hftp, netinfo* pnetinfo)//test FTP connection
{
	CString sTmp;
	BOOL success = FALSE;
	Success = FtpCommand (Hftp, FALSE, Ftp_transfer_type_binary, _t ("NOOP"), (DWORD) pnetinfo, NULL);
	if (!success)
	{
		if (WaitForSingleObject (pnetinfo->m_hevent,1000) = = Wait_object_0)
		{
			resetevent (pnetinfo->m_hevent);
			Success = pnetinfo->m_bsuccess;
			pnetinfo->m_bsuccess = FALSE;
		}
		Else
		{
			success = FALSE;
		}	
	}

	return success;
}

4) Definition of NetInfo structure:

typedef struct TAGNETINFO 
{
	hinternet m_hhandle;
	BOOL m_bsuccess;
	HANDLE m_hevent;
	Tagnetinfo ()
	{
		M_hhandle  = NULL;
		m_bsuccess = FALSE;
		M_hevent   = NULL;
	}

} NETINFO;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.