![]() |
![]() |



Author: firefox
Date Submitted: 2004-01-11 16:59:52
/*
==========================
winsock_TcpRead
Reads bytes off the network.
returns the number of bytes we got. <0 is an error
If it successfully reads data from the network, address will
hold the address of the machine that sent the data
==========================
*/
int winsock_TcpRead(int socket, byte* buf, int len, netaddress_t* address)
{
int bytesread, totalbytesread;
byte *b2;
totalbytesread = 0;
b2 = buf;
do{
bytesread = recv(socket, b2, len - totalbytesread, 0);
b2 += bytesread;
totalbytesread += bytesread;
}while((bytesread != SOCKET_ERROR) && bytesread!=0 && bytesread != WSAECONNRESET && totalbytesread<len);
// convert the returned address
return bytesread;
}Up there you can find a corrected version of winsock_TcpRead. The current version produces totally strange output when reading bigger chunks of data via TCP.
The reason for this was a wrong buffer handling. Instead of adding all the Input received by the recv function, the buffer was overwritten everytime, therefore containing the last chunk of data received instead of the full data.
I noticed this when using the REALLY great URL Retrieval Code Snippet by RichardGreen. Thanks Richard, great work!
[Recent Contributions] [Recent Source Code]
User Contributed Comments