CSocketWrench::Read Method  
 
INT Read(
  LPBYTE lpBuffer,  
  INT cbBuffer  
);
INT Read(
  LPBYTE lpBuffer,  
  INT cbBuffer,  
  LPINTERNET_ADDRESS lpAddress,  
  UINT* lpnRemotePort  
);

The Read method reads the specified number of bytes from the socket and copies them into the buffer. The data may be of any type, and is not terminated with a null character.

Parameters

lpBuffer
Pointer to the buffer in which the data will be stored.
cbBuffer
The maximum number of bytes to read and copy into the specified buffer. This value must be greater than zero.
lpAddress
Pointer to an INTERNET_ADDRESS structure that which will contain the IP address of the remote host that sent the data being read. If this information is not required, the parameter may be specified as NULL.
lpnRemotePort
Pointer to an unsigned integer which will contain the remote port number. If this information is not required, the parameter may be specified as NULL.

Return Value

If the method succeeds, the return value is the number of bytes actually read. A return value of zero indicates that the remote host has closed the connection and there is no more data available to be read. If the method fails, the return value is INET_ERROR. To get extended error information, call GetLastError.

Remarks

The Read method will read up to the specified number of bytes and store the data in the buffer provided by the caller. If there is no data available to be read at the time this method is invoked, the session thread will block until at least one byte of data becomes available, the timeout period elapses or an error occurs. This method will return if any amount of data is sent by the remote host, and will not block until the entire buffer has been filled. To avoid blocking the current thread, either create an asynchronous socket or use the IsReadable method to determine if there is data available to be read prior to calling this function.

The application should never make an assumption about the amount of data that will be available to read. TCP considers all data to be an arbitrary stream of bytes and does not impose any structure on the data itself. For example, if the remote host is sending data to the server in fixed 512 byte blocks of data, it is possible that a single call to the Read method will return only a partial block of data, or it may return multiple blocks combined together. It is the responsibility of the application to buffer and process this data appropriately.

For applications that are built using the Unicode character set, it is important to note that the buffer is an array of bytes, not characters. If the remote host is sending string data to the server, it must be read as a stream of bytes and converted using the MultiByteToWideChar function. If the remote host is sending lines of text terminated with a linefeed or carriage return and linefeed pair, the ReadLine method will return a line of text at a time and perform this conversion for you.

When Read is called and the socket is in non-blocking mode, it is possible that the method will fail because there is no available data to read at that time. This should not be considered a fatal error. Instead, the application should simply wait to receive the next asynchronous notification that data is available.

Requirements

Minimum Desktop Platform: Windows 7 (Service Pack 1)
Minimum Server Platform: Windows Server 2008 R2 (Service Pack 1)
Header: Include cswsock10.h
Import Library: cswskv10.lib

See Also

EnableEvents, IsBlocking, IsReadable, IsWritable, Peek, ReadLine, RegisterEvent, Write, WriteLine