The WriteStream method enables an application to write an
arbitrarily large stream of data from memory or a file to the
specified socket. Unlike the Write method, which may not
write all of the data in a single method call, WriteStream
will only return when all of the data has been written or an error
occurs.
This method will force the thread to block until the operation
completes. If this method is called with asynchronous events
enabled, it will automatically switch the socket into a blocking
mode, write the data stream and then restore the socket to
asynchronous operation when it has finished. If another socket
operation is attempted while WriteStream is blocked sending
data to the remote host, an error will occur. It is recommended
that this method only be used with blocking (synchronous) socket
connections; if the application needs to establish multiple
simultaneous connections, it should create worker threads to manage
each connection.
It is possible for some data to have been written even if the
method returns a value of zero. Applications should also check the
value of the lpdwLength argument to determine if any data
was sent. For example, if a timeout occurs while the method is
waiting to write more data, it will return zero; however, some data
may have already been written to the socket prior to the error
condition.
Because WriteStream can potentially cause the application
to block for long periods of time as the data stream is being
written, the method will periodically generate INET_EVENT_PROGRESS
events. An application can register an event handler using the
RegisterEvent method, and can obtain information about the
current operation by calling the GetStreamInfo method.
CFile *pFile = new CFile();
DWORD dwLength = 0;
if (!pFile->Open(strFileName, CFile::modeRead | CFile::shareDenyWrite))
return;
dwLength = pFile->GetLength();
if (dwLength > 0)
{
BOOL bResult = pSocket->WriteStream(
pFile->m_hFile,
&dwLength,
INET_STREAM_HANDLE);
}
delete pFile;