FTP异步模型测试网络状态

xiaoxiao2021-02-28  108

开启FTP测试线程:

1、线程声明:

static DWORD WINAPI FtpCheckThread(LPVOID lpParam); 2、线程开启:

CloseHandle(CreateThread( NULL,0,FtpCheckThread,this,0,NULL)); 3、线程实现:

InternetOpen最后一个参数设置为异步模型。

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, //手动 FALSE, //初始无信号 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);//设置回调函数 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("第一次连接就成功了!")); gstrNetWorkMsg = _T("网络连接成功!"); ::PostMessage(pThis->m_hWnd,WM_NETSTATEMSG,0,0); }else{ if (WaitForSingleObject(pNetInfo->m_hEvent,10000) == WAIT_OBJECT_0) { ResetEvent(pNetInfo->m_hEvent); hFtp = pNetInfo->m_hHandle; pNetInfo->m_hHandle = NULL; } if (hFtp == NULL) { //网络连接失败,正尝试重新连接 gstrNetWorkMsg = _T("网络连接失败,正尝试重新连接..."); ::PostMessage(pThis->m_hWnd,WM_NETSTATEMSG,0,0); } else { //网络连接成功。。。 gstrNetWorkMsg = _T("网络连接成功!"); ::PostMessage(pThis->m_hWnd,WM_NETSTATEMSG,0,0); Sleep(1000); bSuccess = FtpCommadCheck(hFtp,pNetInfo); if (bSuccess) { //NetWork Ok gstrNetWorkMsg = _T("NetWork Ok!"); ::PostMessage(pThis->m_hWnd,WM_NETSTATEMSG,0,0); } else { //NetWork Error gstrNetWorkMsg = _T("NetWork Error!"); ::PostMessage(pThis->m_hWnd,WM_NETSTATEMSG,0,0); } } Sleep(1000); } delete[] pChar; //...... }else{ Sleep(1000); } Sleep(10000); } return 1; } 其中:FtpCheckThread()函数中涉及到的几个函数的实现如下:

1)回调函数的声明和实现:

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)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)FtpCommadCheck函数的实现:

BOOL FtpCommadCheck(HINTERNET hFtp, NETINFO* pNetInfo)//测试FTP连接 { 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)NETINFO结构的定义:

typedef struct tagNetInfo { HINTERNET m_hHandle; BOOL m_bSuccess; HANDLE m_hEvent; tagNetInfo() { m_hHandle = NULL; m_bSuccess = FALSE; m_hEvent = NULL; } }NETINFO;

转载请注明原文地址: https://www.6miu.com/read-20713.html

最新回复(0)