Archived

This topic is now archived and is closed to further replies.

kuphryn

Data Format via CString and/or string :: Winsock

Recommended Posts

Hi. I am implementing an overlapped I/O based Winsock program. I would like to know how to format the data buffer after WSARecv returns. For example: ----- // Allocate 100 bytes. TCHAR *data = new data[100 + 1]; data[100] = NULL; // Link data buffer and size to WSABUF. WSABUF wBuf; wBuf.len = 10000; wBuf.buf = data; ... WSARecv(..., &wBuf,...,); ... ----- Given WSARecv returns after sometime with the data, how do you format, 20 bytes of valid data. In other words, data could contain 100 bytes of data, however, what if only 20 bytes are valid and the rest are random data Winsock produces from the socket? I tried using CString and trim(), but it does not seem to help under an overlapped I/O. For example: ----- WSARecv(...); ... CString validData = CString(data); validData.trim(); ----- The solution above does not work for overlapped I/O. Even with trim(), validData will hold some valid data and a whole lot of random bytes. I would like to know how to format a raw byte data buffer into valid characters only. Thanks, Kuphryn

Share this post


Link to post
Share on other sites
all send/receive functions return the number of bytes actually transfered. it will be in lpNumberOfBytesRecvd for WSARecv, in lpcbTransfer for WSAGetOverlappedResult, and in cbTransfered parameter to your completion routine. that number tells you how much of receive buffer is actually filled with valid data.

Share this post


Link to post
Share on other sites