Microsoft KB Archive/192704

= PRB: CArchive::Read Might Hang When Using CSocketFile =

Article ID: 192704

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2

 Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription</li></ul>

 Microsoft Visual C++ 4.2 Enterprise Edition</li></ul>

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q192704

<div class="symptoms_section">

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.

<div class="cause_section">

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.

<div class="resolution_section">

RESOLUTION
The underlying socket of the socket file should be closed at the end of transmitting the data.

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

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.

Additional query words: Write hangs CAsyncSocket

Keywords: kbfileio kbprb kbwinsock KB192704

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.