Send binary data with ICE

Ice provides a communication solution that is simple to understand and easy to program with. Yet, despite its simplicity, Ice is flexible enough to accommodate even the most demanding and mission-critical applications.

ICE framework has a few primitive types to send data with the interfaces. Many times you need send binary data as JPG images, objects, music files, etc.The following source (C++) show how you can send easily binary data with ICE.

Firstly, define your own interface. Be sure to get a ByteSeq field, we use this field to save and send the binary data.

class Data {
	ByteSeq binary;
interface Communicator {
	void notify (Data data);	

The client part just read the binary data (file or memory) and copy in std::vector. Then you have to do memcpy to ByteSeq field. You notice that ByteSeq type is translated to std::vector in C++

int MAX_BUFFER_READ = 100*1024; // 100 Kb
std::vector buffer;
std::string filename = "/tmp/image.jpg";
ifstream input(filename);
if (input)
	buffer.resize( MAX_BUFFER_READ ); (char*)(&buffer[0]), MAX_BUFFER_READ ) ;
	size_t counted = input.gcount();
	buffer.resize (counted);
	DataPtr mydata = new Data();
	memcpy(&(myData->binary[0]),(unsigned char *) &buffer[0], buffer.size());

The server has to receive the data and save in file. This example sends a JPEG image file, but if you want you can send a serialized object to restore it in memory.

virtual void notify (const DataPtr& data , const Ice::Current& current )
	std::string filename = "/tmp/image2.jpg";
	ofstream of( filename.c_str() );
	if (of)
		of.write(reinterpret_cast&<char*>(&data->binary[0]), data->binary.size());

Remember that we have an ICE limitation in order to size. The max size allowed is 10 MBytes, so if you want send bigger files you will have to create a bit protocol for that.

facebooktwittergoogle_pluslinkedinmailby feather
This entry was posted in jderobot. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *