PRB: CArchive::Read Might Hang When Using CSocketFile (192704)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++, 32-bit Editions 2.1
- Microsoft Visual C++, 32-bit Editions 2.2
- Microsoft Visual C++, 32-bit Editions 4.0
- Microsoft Visual C++, 32-bit Editions 4.1
- Microsoft Visual C++, 32-bit Enterprise Edition 4.2
- Microsoft Visual C++, 32-bit Professional Edition 4.2
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q192704 SYMPTOMS
When reading data that uses CArchive::Read and the file is a CSocketFile,
certain buffer sizes and data sizes are more likely to cause a hang in
CSocket::Receive at the end of transmission when the transmission isn't
closed.
CAUSE
CArchive sometimes tries to maintain data in its buffer even if all of the
data requested has been read. This causes an extra CSocketFile::Read call
when no data is available on the socket file. The Read call hangs until
more data appears or until the socket connection is closed.
RESOLUTION
The underlying socket of the socket file should be closed at the end of
transmitting the data.
STATUS
This behavior is by design.
MORE INFORMATION
CArchive provides a buffered mechanism to read data from a file, which can
be based on CSocketFile as well as CFile. The algorithm for CArchive::Read
is implemented so that an extra call to a CFile derived class (for example,
CSocketFile) Read function is made even if all of the needed data is
present. For disk-based files, this presents no problem because EOF is
reached and the call to Read returns immediately. For CsocketFile-based
files, either the socket must be closed or more data needs to be sent for
the Read call to return.
The hang is more likely to occur if the transmitted data size is a multiple
of the buffer size (4k) because the last byte needed is more likely to be
the last byte in the current buffer. This results in an empty buffer and
causes another Read operation even though there is no more data to be read.
Normally, if the required data to be read is less than the buffer size, the
Read operation returns with less than the requested size (the buffer size),
however in this case, it does not cause CArchive to replenish its buffer.
The proper way to avoid this situation is to close the socket connection.
(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Adam
Kim, Microsoft Corporation.
Modification Type: | Major | Last Reviewed: | 12/2/2003 |
---|
Keywords: | kbFileIO kbprb kbWinsock KB192704 |
---|
|