Thread: WSARecv and IOCP :: Winsock

  1. #1
    Registered User
    Join Date
    Nov 2001

    WSARecv and IOCP :: Winsock


    I am working on a network client and one of its features is download files over HTTP server, i.e. The program downloads some files successfully, but it fails to download some files. For some reason, it download the all files, but some files are unvalid in terms of CRC.

    I understand that the server initially sends information about a file including its size and type (Content-Type). However, for some reason files that have bad CRC seem to miss the beginning chunk of data. In other words, the program does the file, but the first twenty bytes might contain bad CRC. Here is the algorithm of the design.

    - Send request (GET)
    - Call WSARecv
    - On first incoming data, analyze data and determine file size.
    - Analyze data and save any valid data that server might have including with file information.
    - Call WSARecv
    - Continue calling WSARecv and appending new data until size of data equals the size that the server sent.

    I would like to know is it possible that the server sends out the data before it sends out information about the data such as size? With the algorithm above, the program can download some file, but fails other files in terms of CRC. I cannot understand how that is possible because the size of the file is correct most of the time, but even so the files have bad CRC. Another interesting fact is that the bad CRC always occurs at the beginning such as the first 20 bytes.


  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    >>is it possible that the server sends out the data before it sends out information about the data such as size?
    Not really, imho. The server will send things out in the correct order, and the TCP protocol will ensure the packets get to you in the same order. So unless the server is screwy, I'd say this wasn't your problem.

    Does it always mess up the same file?
    Have you managed to work out what data it should have written, is there any pattern between that and the data it actually wrote?
    You could do a network trace to watch the data going to your PC.
    Is it possible you've mis-interpretting the header, starting to write data to the file at the wrong point in the process?
    I suggest you create a second file, and output everything you receive to it, headers and all. That process would be simpler, as you don't need to parse the header. With this, you may be able to spot your problem.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2001
    Okay. Thanks.

    The problem has to do with the fact that the program used a pointer to the actual IOCP buffer to write to the file. WriteFile corrupted the buffer. One solution is to use memcpy() to copy the most current buffer to temporary memory and then write it to disk.


  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    Interesting. Any idea why WriteFile corrupted the data like that?
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  5. #5
    Registered User
    Join Date
    Nov 2001
    I think WriteFile keeps IOCP from updating the buffer with incoming data.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. AcceptEx with WSASend and WSARecv :: Winsock
    By kuphryn in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-05-2003, 08:48 PM