c++ - Boost Client-Server Connection Stability -
i quite new in setting client-server connections , have problem sending info between 2 computers. code working "localhost" 2 computers on network, crashes somewhere. in localhost, can transfer 22 mb data, largest info file.
for 2 computers connection: have little illustration transfer info , info amount of around 11 kb. illustration working here. however, next bigger illustration size 0f 230 kb crashes.
i using synchronous tcp.
server:
seek { boost::asio::io_service io_service; tcp::acceptor acceptor( io_service, tcp::endpoint( tcp::v4( ), 40986 ) ); cdbreader* sender = cdbreader::getinstance( ); ( ;; ) { tcp::socket socket( io_service ); mysocket = &socket; acceptor.accept( *mysocket ); }
client:
try { boost::asio::io_service io_service; tcp::resolver resolver( io_service ); tcp::resolver::query query( servername, "40986" ); tcp::resolver::iterator endpoint_iterator = resolver.resolve( query ); tcp::socket socket( io_service ); mysocket = &socket; boost::asio::connect( *mysocket, endpoint_iterator ); boost::system::error_code error; }
this how create connection.
and fuction used in server send data. acc amount of data, sending after dividing parts.
void vtkcdbconnectorsource::sendstring( std::string message ) { std::cout << "i in sendstring" << std::endl; int loopcounter = message.size() / chararraysize + 1; size_t msize = message.size(); std::stringstream sstream; sstream << loopcounter; std::string noloop = sstream.str( ); // std::cout << "loopcounter = " << loopcounter << std::endl; if(loopcounter >= 10 && loopcounter < 100) { std::stringstream digitnumbersizestream; digitnumbersizestream << 2; std::string digitnumbersize = digitnumbersizestream.str( ); digitnumbersizestream.str(std::string()); digitnumbersizestream.clear(); // std::cout << "digitnumbersize = " << digitnumbersize << std::endl; // std::cout << "noloop = " << noloop << std::endl; size_t s10 = mysocket->send( boost::asio::buffer( digitnumbersize ) ); size_t s11 = mysocket->send( boost::asio::buffer( noloop ) ); } else if( loopcounter >= 100 && loopcounter < 1000) { std::stringstream digitnumbersizestream; digitnumbersizestream << 3; std::string digitnumbersize = digitnumbersizestream.str( ); digitnumbersizestream.str(std::string()); digitnumbersizestream.clear(); // std::cout << "digitnumbersize = " << digitnumbersize << std::endl; // std::cout << "noloop = " << noloop << std::endl; size_t s10 = mysocket->send( boost::asio::buffer( digitnumbersize ) ); size_t s11 = mysocket->send( boost::asio::buffer( noloop ) ); } else if( loopcounter >= 1000) { std::stringstream digitnumbersizestream; digitnumbersizestream << 4; std::string digitnumbersize = digitnumbersizestream.str( ); digitnumbersizestream.str(std::string()); digitnumbersizestream.clear(); // std::cout << "digitnumbersize = " << digitnumbersize << std::endl; // std::cout << "noloop = " << noloop << std::endl; size_t s10 = mysocket->send( boost::asio::buffer( digitnumbersize ) ); size_t s11 = mysocket->send( boost::asio::buffer( noloop ) ); } else { std::stringstream digitnumbersizestream; digitnumbersizestream << 1; std::string digitnumbersize = digitnumbersizestream.str( ); digitnumbersizestream.str(std::string()); digitnumbersizestream.clear(); // std::cout << "digitnumbersize = " << digitnumbersize << std::endl; // std::cout << "noloop = " << noloop << std::endl; size_t s10 = mysocket->send( boost::asio::buffer( digitnumbersize ) ); size_t s11 = mysocket->send( boost::asio::buffer( noloop ) ); } sstream.str( std::string( ) ); sstream.clear( ); int = 0; while( < loopcounter ) { int secloop = 0; if( msize >= chararraysize) { secloop = chararraysize; msize = msize - secloop; } else { secloop = msize; } std::string tempmessage; for( size_t j=0; j<secloop; j++) { tempmessage.push_back( message[chararraysize*i + j] ); } std::stringstream messagesizestream; messagesizestream << secloop; std::string messagesize = messagesizestream.str( ); size_t datadigitsize = messagesizestream.str().size(); std::stringstream messagedigitstream; messagedigitstream << datadigitsize; std::string messagedigit = messagedigitstream.str( ); // std::cout << "messagedigit = " << messagedigit << std::endl; // std::cout << "messagesize = " << messagesize << std::endl; size_t s12 = mysocket->send( boost::asio::buffer( messagedigit ) ); size_t s13 = mysocket->send( boost::asio::buffer( messagesize ) ); size_t s14 = mysocket->send( boost::asio::buffer( tempmessage ) ); messagesizestream.str( std::string( ) ); messagesizestream.clear( ); ++i; } // std::cout<<"sendstring finished"<<std::endl; mysendstream.str( std::string( ) ); mysendstream.clear( ); }
and function in client side receive data:
size_t vtkcdbconnectorsource::datacontainer( ) { std::cout << "i in datacontainer" << std::endl; char* sizeofloop = new char[10]; char* noofloops= new char[100]; size_t datalength; size_t loopsizedigit = mysocket->receive( boost::asio::buffer( sizeofloop, 1 ) ); std::stringstream lstream; lstream << sizeofloop; size_t numberofloops; lstream >> numberofloops ; lstream.str(std::string()); lstream.clear(); std::cout<<"number of digits loops= "<<numberofloops<<std::endl; size_t sizedigit = mysocket->receive( boost::asio::buffer(noofloops, numberofloops ) ); std::stringstream rstream; rstream << noofloops; size_t loopsize; rstream >> loopsize ; rstream.str(std::string()); rstream.clear(); std::cout << "loopsize = " << loopsize << std::endl; int = 0; while( < loopsize ) { char* datadigit= new char[100]; size_t sizedatadigit = mysocket->receive( boost::asio::buffer( datadigit, 1 ) ); rstream << datadigit; size_t digitnumber; rstream >> digitnumber; rstream.str(std::string()); rstream.clear(); char* datasize = new char[100]; // std::cout << "digitnumber = " << digitnumber << std::endl; size_t sizedata = mysocket->receive( boost::asio::buffer( datasize, digitnumber ) ); rstream << datasize; size_t dataamount; rstream >> dataamount; rstream.str(std::string()); rstream.clear( ); char* data= new char[100000]; // std::cout << "dataamount = " << dataamount << std::endl; datalength = mysocket->receive( boost::asio::buffer( data, dataamount ) ); std::cout << "datalength = " << datalength << std::endl; int k = 0; while ( datalength-- > 0 ) // maybe need split 2 { myreceivedstream << data[k++]; } delete[] data; delete[] datadigit; delete[] datasize; ++i; } delete[] sizeofloop; delete[] noofloops; std::cout << " ---- info container finished--- " << std::endl; homecoming datalength; }
i hope can tell problem, if there point unclear, please tell me know. in advance help.
this code undefined behavior:
tcp::socket socket( io_service ); mysocket = &socket;
you're creating tcp::socket
in scope of try
block, storing address of in mysocket
, exists outside of scope. 1 time leave try
block, tcp::socket
destroyed , mysocket
still pointing destroyed object.
it may work while, until other code runs , overwrites portion of stack destroyed tcp::socket
object occupying.
once prepare this, same problem applies io_service
; build socket reference it, allow destroyed, fixed tcp::socket
object still holding reference destroyed io_service
.
c++ boost tcp
No comments:
Post a Comment