boost::asio write_some

B

biernat.przemyslaw

I have some problem with send data by boost::asio. My code:

void Connection::SendCommand(std::shared_ptr<ICommand> command)
{
int dataSize = command->GetSize();

vector<char> dataBuffer = vector<char>(dataSize);

stringstream ss;
binary_oarchive oa(ss);
command->Serialize(oa);

if(_side == Server)
command->Execute();

binary_iarchive ia(ss);

ia.load_binary(&dataBuffer[0], dataSize);

int* header = new int[3];

header[0] = (int)(command->Type());
header[1] = (int)FromClientToServer;
header[2] = dataSize;

size_t s = _socket->write_some(buffer(header, 3*sizeof(int)));
size_t s1 = _socket->write_some(buffer(dataBuffer, dataSize*sizeof(char)));

delete[] header;
}

I get exception on the second call write_some:

boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x0021F520.

Does anybody know what is wrong?
 
J

James Kuyper

I have some problem with send data by boost::asio. My code:

void Connection::SendCommand(std::shared_ptr<ICommand> command)
{
int dataSize = command->GetSize();

vector<char> dataBuffer = vector<char>(dataSize);

stringstream ss;
binary_oarchive oa(ss);
command->Serialize(oa);

if(_side == Server)
command->Execute();

binary_iarchive ia(ss);

ia.load_binary(&dataBuffer[0], dataSize);

int* header = new int[3];

header[0] = (int)(command->Type());
header[1] = (int)FromClientToServer;
header[2] = dataSize;

size_t s = _socket->write_some(buffer(header, 3*sizeof(int)));
size_t s1 = _socket->write_some(buffer(dataBuffer, dataSize*sizeof(char)));

delete[] header;
}

I get exception on the second call write_some:

boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x0021F520.

Does anybody know what is wrong?

You'll get better answers in a forum devoted to C++. Much of that code
is nothing more than a syntax error as far as C is concerned.
 
I

Ike Naar

I have some problem with send data by boost::asio. My code:

void Connection::SendCommand(std::shared_ptr<ICommand> command)
{
int dataSize = command->GetSize();

vector<char> dataBuffer = vector<char>(dataSize);

stringstream ss;
binary_oarchive oa(ss);
command->Serialize(oa);

if(_side == Server)
command->Execute();

binary_iarchive ia(ss);

ia.load_binary(&dataBuffer[0], dataSize);
int* header = new int[3];

header[0] = (int)(command->Type());
header[1] = (int)FromClientToServer;
header[2] = dataSize;

size_t s = _socket->write_some(buffer(header, 3*sizeof(int)));
size_t s1 = _socket->write_some(buffer(dataBuffer, dataSize*sizeof(char)));

delete[] header;
}

I get exception on the second call write_some:

boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x0021F520.

Does anybody know what is wrong?

Note that boost::asio::write_some is called asynchronously: it may
return before the buffer is completely written.
The caller must make sure that the buffer's underlying memory blocks
are retained until the write completes. This does not seem to be
the case in your program: 'dataBuffer' (a local variable) may have
been destroyed before the write completes.

What happens if you use boost::asio::write (which blocks until all
data have been written), instead of boost::asio::write_some ?
 
B

biernat.przemyslaw

I have some problem with send data by boost::asio. My code:

void Connection::SendCommand(std::shared_ptr<ICommand> command)

int dataSize = command->GetSize();

vector<char> dataBuffer = vector<char>(dataSize);

stringstream ss;
binary_oarchive oa(ss);
command->Serialize(oa);

if(_side == Server)
command->Execute();

binary_iarchive ia(ss);
ia.load_binary(&dataBuffer[0], dataSize);


int* header = new int[3];
header[0] = (int)(command->Type());
header[1] = (int)FromClientToServer;
header[2] = dataSize;
size_t s = _socket->write_some(buffer(header, 3*sizeof(int)));
size_t s1 = _socket->write_some(buffer(dataBuffer, dataSize*sizeof(char)));
delete[] header;

I get exception on the second call write_some:
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> > at memory location 0x0021F520.
Does anybody know what is wrong?



Note that boost::asio::write_some is called asynchronously: it may

return before the buffer is completely written.

The caller must make sure that the buffer's underlying memory blocks

are retained until the write completes. This does not seem to be

the case in your program: 'dataBuffer' (a local variable) may have

been destroyed before the write completes.



What happens if you use boost::asio::write (which blocks until all

data have been written), instead of boost::asio::write_some ?

If write_some is called asynchronously, how will be called async_write_some. I mean that write_some is synchronously against async_write_some is asynchronously. I haven't try boost::asio::write yet, but I will try soon.
 
N

Noob

biernat.przemyslaw said:
I have some problem with send data by boost::asio.

"Boost is a set of libraries for the C++ programming language [...]"

This is a C newsgroup. You want comp.lang.c++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top