Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > boost::asio write_some

Reply
Thread Tools

boost::asio write_some

 
 
biernat.przemyslaw@gmail.com
Guest
Posts: n/a
 
      06-29-2013
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::excepti on_detail::error_info_injector<boost::system::syst em_error> > at memory location 0x0021F520.

Does anybody know what is wrong?
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      06-29-2013
On 06/29/2013 02:45 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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::excepti on_detail::error_info_injector<boost::system::syst em_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.
--
James Kuyper
 
Reply With Quote
 
 
 
 
Ike Naar
Guest
Posts: n/a
 
      06-30-2013
On 2013-06-29, (E-Mail Removed) <(E-Mail Removed)> wrote:
> 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::excepti on_detail::error_info_injector<boost::system::syst em_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 ?
 
Reply With Quote
 
biernat.przemyslaw@gmail.com
Guest
Posts: n/a
 
      07-01-2013

> > 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::excepti on_detail::error_info_injector<boost::system::syst em_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.
 
Reply With Quote
 
Noob
Guest
Posts: n/a
 
      07-01-2013
biernat.przemyslaw wrote:

> 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++

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boost::Asio synchronous I/O operation with timeout Antonio Di Monaco C++ 0 04-14-2012 08:20 AM
boost::thread and boost::asio::io_service questions Christopher C++ 0 06-08-2011 03:55 PM
boost::asio for HTTP -> data truncated Torsten Mueller C++ 3 08-13-2010 08:18 AM
boost::asio (Permission denied, Connection refused) wo3kie@gmail.com C++ 1 04-20-2008 02:52 AM
How to compile the HelloWorld of boost.asio? =?utf-8?B?5byg5rKI6bmP?= C++ 1 08-10-2006 04:37 AM



Advertisments