In general, the problem is this: I'm trying to get data from a text file on the server (~ 10 bytes) using WinInet in such a way that:
- There is a function that returns the contents of the file.
- A new thread is created with this function in a looped form, with a break of 10 seconds.
The request is sent, the answer comes, but if the contents of the file has changed - the answer for some reason does not change.
Here is the code itself:
#include <iostream> #include <windows.h> #include <wininet.h> #include <thread> #pragma comment(lib,"wininet") using namespace std; string flag; string getresponse(){ //сама функция получения ответа string ret; bool ok = false; HINTERNET hInternet = ::InternetOpen( TEXT("WinInet Test"), INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL, 0); if (hInternet != NULL) { HINTERNET hConnect = ::InternetConnect( hInternet, TEXT("www.applang.tk"), INTERNET_DEFAULT_HTTP_PORT, NULL,NULL, INTERNET_SERVICE_HTTP, 0, 1u); if (hConnect != NULL) { HINTERNET hRequest = ::HttpOpenRequest( hConnect, TEXT("GET"), TEXT("baget/com"), NULL, NULL, 0, INTERNET_FLAG_KEEP_CONNECTION, 1); if (hRequest != NULL) { BOOL bSend = ::HttpSendRequest(hRequest, NULL,0, NULL,0); if (bSend) { for (;;) { char szData[1024]; DWORD dwBytesRead; BOOL bRead = ::InternetReadFile( hRequest, szData,sizeof(szData)-1, &dwBytesRead); if (bRead == FALSE || dwBytesRead == 0) break; szData[dwBytesRead] = 0; ret = string(szData,strlen(szData)); ok = true; } } ::InternetCloseHandle(hRequest); } ::InternetCloseHandle(hConnect); } ::InternetCloseHandle(hInternet); } return ret; } void syncserv(string arg){ //вспомогательная функция while(true){ cout<<"SENT REQUEST"<<endl; flag = getresponse(); cout <<"GOT RESPONSE: "<<flag<<endl; Sleep(10000); } } main(){ thread t1(syncserv, "something"); //создаем поток getch(); }