Thread: problem with previously working internet code

  1. #1
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195

    problem with previously working internet code

    This code previously worked on a ... 'project' of mine. Now though it fails to send request and returns error 997 (IO_PENDING).

    Code:
    // ........ Kuwait.cpp : Defines the entry point for the console application.
    //
    #include <windows.h>
    #include <wininet.h>
    #include <stdio.h>
    BYTE pBuffer[1048576];
    int main(int argc, char* argv[]){
        DWORD BytesRead = 0;
        printf("Opening internet...");
     HINTERNET hInternet = InternetOpen(L"FreeSpeech" , INTERNET_OPEN_TYPE_DIRECT , NULL , NULL , INTERNET_FLAG_ASYNC);
        if(hInternet != NULL){
            printf("done.\nOpening connection...");
            } else {
            printf("FAILED.\n");
            return 0;
            }
        HINTERNET hConnect = InternetConnect(hInternet , L"134.225.36.80" , INTERNET_DEFAULT_HTTP_PORT , L"anonymous" , NULL , INTERNET_SERVICE_HTTP , 0 , NULL);
        // http://www.unitednetworks.com.kw
        if(hConnect != NULL){
            printf("done.\nOpening request...");
            } else {
            printf("FAILED.\n");
            return 0;
            }
        HINTERNET hRequest = HttpOpenRequest(hConnect , NULL , L"134.225.36.80/integers/?num=10&min=1&max=6&col=1&base=10&format=plain&rnd=new" , NULL , NULL , NULL , INTERNET_FLAG_RELOAD , NULL);
        if(hRequest != NULL){
            printf("done.\n");
            } else {
            printf("FAILED.\n");
            return 0;
            }
    start:
        BOOL bSend = HttpSendRequest(hRequest , NULL , -1L , NULL , 0);
        if(bSend){
            printf("Request Sent.\n");
            } else {
            printf("Failed to send request.\n");
            printf("Errorcode = %d\n" , GetLastError());
      Sleep(1000);
            goto start; //return 0;
            }
        BOOL bRead;
        while(BytesRead == 0){
            bRead = InternetReadFile(hRequest , pBuffer , 1048576 , &BytesRead);
            Sleep(0);
            }
        printf("%d bytes read.\n" , BytesRead);
        BOOL bClose = InternetCloseHandle(hRequest);
        
        FILE* pFile = fopen("test.txt" , "w+b");
        fwrite((VOID*)pBuffer , 1048576, 1 , pFile);
        fclose(pFile);
        
        return 0;
        }

  2. #2
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    The problem isn't really with the code. You have a pending transaction. Remember you're using Async comms, transactions don't have to wait for a return on a pending operation.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by WDT View Post
    The problem isn't really with the code. You have a pending transaction. Remember you're using Async comms, transactions don't have to wait for a return on a pending operation.
    Suggestions? Because I'm not getting any data returned even if I proceed on this error. Wiating on the hRequest returns invlid handle so that wont work either.
    Last edited by abachler; 04-05-2009 at 06:35 PM.

  4. #4
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    You can't proceed on the error. As you have an operation waiting.
    The problem exists with your connection. My advice (it has been a long time sinceI've done TCP/IP programming) is to:
    a) Test it on a data server that you know will respond immediately (I used to have a test server running on another machine locally with token data)

    b) (I've just quickly browsed through the WIN Inet functions here.) Diagnose the connection. Use the HttpQueryInfo() to find out about the server connection. <== The more logical advice


    Also can I see a print out of your output up unto the point of failure??
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Here is the output. Querying the HTTP_QUERY_STATUS_CODE (19) returns 48, opening the page direcly in a browser works fine.
    Last edited by abachler; 04-06-2009 at 01:47 AM.

  6. #6
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Hhhmm... This is a very interesting problem. I've spent 20 mins or so digging on the internet and all it reminded me of was why I got absolutely horrified when I was doing TCP/IP programming Windows style back then. (I actually quit TCP/IP programming after doing this) But I love a challenge.
    I'll give you an answer tonight after I've run the code myself; also I came across something that says your HTTPSendRequest function must be used within a Callback function.
    I also found no 48 in the HTTP STATUS CODES LIST (19).
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by WDT View Post
    also I came across something that says your HTTPSendRequest function must be used within a Callback function.
    I also found no 48 in the HTTP STATUS CODES LIST (19).
    There is no such requirement listed on msdn that I saw.

    There is no code 48, its obviously another exmaple of an undocumented code, MS is good for that crap.

  8. #8
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    can I just ask what IDE you're using?? I tried compilin it in VC++2008 express but can't get the option to link libraries/add references (I'm new to the studio express IDE).
    I compiled it in Dev C++ but I get a "NO HOST APPLICATION WAS PROVIDED" message.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by WDT View Post
    can I just ask what IDE you're using?? I tried compilin it in VC++2008 express but can't get the option to link libraries/add references (I'm new to the studio express IDE).
    I compiled it in Dev C++ but I get a "NO HOST APPLICATION WAS PROVIDED" message.
    Its under prject->properties->linker->inputs->additional dependencies

  10. #10
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Ok I' tried the other night (Sorry I didn't get back to you) but The VC++ IDE Major Headache atm has a problem with the .obj file so i can't compile your code to run. I suspect maybe a Win7 issue.
    Anyways, the best I can give you is that you either have a firewall issue on one side of the connection which is interfering with the connection (this has happened to me before) or the server's ability to reply. This is probably most likely as a web browser is recognised by the FW but your custom program wouldn't be; but without the correct HTTP_STATUS query code (Are you sure it's not 408?? because it looks a 408 symptom) can't say for sure.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    1
    Using WinInet Asynchrousnly is a PITA. Use 0 instead INTERNET_FLAG_ASYNC in the InternetOpen Calll and that should fix the problem.

    Code:
    HINTERNET hInternet = InternetOpen(L"FreeSpeech" , INTERNET_OPEN_TYPE_DIRECT , NULL , NULL , 0);
    For more details on using WinINet in async mode see here

  12. #12
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Quote Originally Posted by studguy1 View Post
    Using WinInet Asynchrousnly is a PITA. Use 0 instead INTERNET_FLAG_ASYNC in the InternetOpen Calll and that should fix the problem.
    Won't argue that; and certainly every document I've read about WinInet Asynchronous API would suggest every single person crops up with a problem somewhere down the implementation stack. Does using Zero make it an asynchronous conection instead then?
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. Problem with my rotation code?
    By EvBladeRunnervE in forum Game Programming
    Replies: 44
    Last Post: 12-21-2003, 12:33 AM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. MFC Multi-threading is working ... Now another problem :(
    By SyntaxBubble in forum Windows Programming
    Replies: 3
    Last Post: 11-13-2003, 08:39 PM
  5. Big Code, Little Problem
    By CodeMonkey in forum Windows Programming
    Replies: 4
    Last Post: 10-03-2001, 05:14 PM