ClientHandler/ClientHandler.cpp
bool ClientHandler::readFile(const std::string filename, size_t fileSize, Socket& socket, const std::string path)
{
	std::string fqname = path + filename;
	FileSystem::File file(fqname);
	file.open(FileSystem::File::out, FileSystem::File::binary);
	if (!file.isGood()) return false;

	const size_t BlockSize = 2048;
	Socket::byte buffer[BlockSize];
	size_t bytesToRead;

	while (true)
	{
		if (fileSize > BlockSize)
			bytesToRead = BlockSize;
		else
			bytesToRead = fileSize;

		socket.recv(bytesToRead, buffer);

		FileSystem::Block blk;
		for (size_t i = 0; i < bytesToRead; ++i)
			blk.push_back(buffer[i]);

		file.putBlock(blk);
		if (fileSize < BlockSize)
			break;
		fileSize -= BlockSize;
	}
	file.close();
	return true;
}
//----< read a binary file from socket and save >--------------------
/*
* This function expects the sender to have already send a file message,
* and when this function is running, continuosly send bytes until
* fileSize bytes have been sent.
*/
< read a binary file from socket and save >
This function expects the sender to have already send a file message,
and when this function is running, continuosly send bytes until
fileSize bytes have been sent.
< read a binary file from socket and save > This function expects the sender to have already send a file message and when this function is running continuosly send bytes until fileSize bytes have been sent .
bool ClientHandler::readFile(const std::string filename, size_t fileSize, Socket& socket, const std::string path)
{
	std::string fqname = path + filename;
	FileSystem::File file(fqname);
	file.open(FileSystem::File::out, FileSystem::File::binary);
	if (!file.isGood()) return false;
	const size_t BlockSize = 2048;
	Socket::byte buffer[BlockSize];
	size_t bytesToRead;
	while (true)
	{
		if (fileSize > BlockSize)
			bytesToRead = BlockSize;
		else
			bytesToRead = fileSize;
		socket.recv(bytesToRead, buffer);
		FileSystem::Block blk;
		for (size_t i = 0; i < bytesToRead; ++i)
			blk.push_back(buffer[i]);
		file.putBlock(blk);
		if (fileSize < BlockSize)
			break;
		fileSize -= BlockSize;
	}
	file.close();
	return true;
}
bool ClientHandler :: readFile ( const std :: string filename , size_t fileSize , Socket & socket , const std :: string path ) { std :: string fqname = path + filename ; FileSystem :: File file ( fqname ) ; file . open ( FileSystem :: File :: out , FileSystem :: File :: binary ) ; if ( ! file . isGood ( ) ) return false ; const size_t BlockSize = 2048 ; Socket :: byte buffer [ BlockSize ] ; size_t bytesToRead ; while ( true ) { if ( fileSize > BlockSize ) bytesToRead = BlockSize ; else bytesToRead = fileSize ; socket . recv ( bytesToRead , buffer ) ; FileSystem :: Block blk ; for ( size_t i = 0 ; i < bytesToRead ; ++ i ) blk . push_back ( buffer [ i ] ) ; file . putBlock ( blk ) ; if ( fileSize < BlockSize ) break ; fileSize -= BlockSize ; } file . close ( ) ; return true ; }
< read a binary file from socket and save >
This function expects the sender to have already send a file message,
and when this function is running, continuosly send bytes until
fileSize bytes have been sent.
< read a binary file from socket and save > This function expects the sender to have already send a file message and when this function is running continuosly send bytes until fileSize bytes have been sent .