本文整理汇总了C++中socket_ptr::read_some方法的典型用法代码示例。如果您正苦于以下问题:C++ socket_ptr::read_some方法的具体用法?C++ socket_ptr::read_some怎么用?C++ socket_ptr::read_some使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket_ptr
的用法示例。
在下文中一共展示了socket_ptr::read_some方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: session
void session(socket_ptr sock)
{
for (;;)
{
char data[max_length];
::memset(data, 0, max_length);
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
// boost::asio::write(*sock, boost::asio::buffer(data, length));
for (int i = 0; i < g_clinets.size(); ++i)
{
boost::asio::write(*g_clinets[i], boost::asio::buffer(data, length));
}
std::cout << "Client Data is :";
std::cout.write(data, length);
std::cout << "\n";
}
}
示例2: session
void session(socket_ptr sock)
{
try
{
for (;;)
{
char data[1024];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break;
else if (error) {
}
cout << "Connection Established" << endl;
string input;
int i = 0;
while (data[i]!='K' || data[i]!='C' || data[i]!='F')
{
input += data[i];
i++;
}
cout << "Calculating";
double val = (double)stoi(input);
if (data[i] == 'F') {
val = ((val - 32) * (5 / 9));
}
if (data[i] == 'K') {
val = val - 273.15;
}
if (data[i] == 'C') {
}
//Convert back to Int
int finalVal = val;
string finalData = to_string(finalVal);
strcpy_s(data, finalData.c_str());
length = strlen(data);
boost::asio::write(*sock, boost::asio::buffer(data, length));
}
}
catch (std::exception& e)
{
}
}
示例3: inboundLoop
void Client::inboundLoop(socket_ptr sock, string_ptr prompt) {
int sizeBufRd = 0;
char buff[1024] = {0};
while( true){
if( sock->available()){
sizeBufRd = sock->read_some( buffer( buff, sizeInput));
string_ptr msg( new string( buff, sizeBufRd));
pMsgQueue->push( msg);
}
// boost::this_thread::sleep( boost::posix_time::millisec(100));
}
}
示例4: session
void session(socket_ptr sock)
{
try
{
for (;;)
{
char data[max_length];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error) {
throw boost::system::system_error(error); // Some other error.
}
std::cout << "Data received.\n";
char temp_type = data[0]; //decode temp unit
std::cout << "Temperature Unit received is: " << temp_type << std::endl;
std::string temp_val_str = "";
for (int i = 1; i < max_length; i++)
{
if (data[i] == 'S') { //no more numbers to convert
break;
}
temp_val_str += data[i];
}
std::cout << "Temperature Value received is: " << temp_val_str << std::endl;
double temp_val = (double)stoi(temp_val_str);
if (temp_type == 'F') {
temp_val = ((temp_val - 32) * (5 / 9)) + 273.15;
}
else if (temp_type == 'C') {
temp_val = temp_val + 273.15;
}
else {
//do nothing
}
std::string client_data = std::to_string(temp_val); //create to client packet
strcpy_s(data, client_data.c_str());
length = strlen(data); //overwrite for new length value
boost::asio::write(*sock, boost::asio::buffer(data, length)); //write modified data back
}
}
catch (std::exception& e)
{
//std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
示例5: getData
string Management::getData(socket_ptr sock){
char data[10000];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
return string("NULL");
else if (error)
throw boost::system::system_error(error);
data[length] = '\0';
cout << "Question: " << data << endl;
return string(data);
}
示例6: inboundLoop
void inboundLoop(socket_ptr sock, string_ptr prompt)
{
int bytesRead = 0;
char readBuf[1024] = {0};
for(;;)
{
if(sock->available())
{
bytesRead = sock->read_some(buffer(readBuf, inputSize));
string_ptr msg(new string(readBuf, bytesRead));
messageQueue->push(msg);
}
boost::this_thread::sleep( boost::posix_time::millisec(1000));
}
}
示例7: inboundLoop
void inboundLoop(socket_ptr sock, string_ptr prompt)
{
std::size_t bytesRead = 0;
char readBuf[1024] = {0};
for(;;)
{
if(sock->is_open() && sock->available())
{
bytesRead = sock->read_some(buffer(readBuf, inputSize));
string_ptr msg(new string(readBuf, bytesRead));
messageQueue->push(msg);
cout << "messages received correctly" << endl;
}
boost::this_thread::sleep( boost::posix_time::millisec(50));
}
}
示例8: session
/// Handling client function
void session(socket_ptr sock) {
std:: cout << "Connected to a client.\n";
try {
for (;;) {
char data[max_length];
boost::system::error_code error;
// Read message from the client
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
// Process data
std::cout << data << std::endl;
db.insert(data); // insert data to the database
// Write message to the client
//boost::asio::write(*sock, boost::asio::buffer(data, length));
}
} catch (std::exception& e) {
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
示例9: session
void session(socket_ptr sock, BobLightServer *boblightServer) {
try {
for (;;) {
char data[1024];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
std::string request(data, length);
boost::algorithm::trim_all(request);
if (request.length() > 0) {
#ifdef DEBUG
std::cout << "net: received " << request.c_str() << "\n";
#endif
std::vector<std::string> strs;
boost::split(strs, request, boost::is_any_of("\n"));
for (unsigned int i = 0; i<strs.size(); ++i) {
std::string response = boblightServer->receive(strs[i]);
if (response.length() > 0) {
#ifdef DEBUG
std::cout << "net: sending " << response.c_str() << "\n";
#endif
boost::asio::write(*sock, boost::asio::buffer(response, response.length()));
}
}
}
else {
usleep(100);
}
}
}
catch (std::exception& e) {
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
示例10: session
void CSocketService::session(socket_ptr sock)
{
try
{
for (;;)
{
char data[max_length];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
boost::asio::write(*sock, boost::asio::buffer(data, length));
}
}
catch (std::exception& e)
{
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
示例11: clientListening
/* For each connection wait for a msg. When it receve a mensage
* call askToServer and write the result for the client */
void GerenciadorServidores::clientListening(socket_ptr sock){
try{
while(true){
char data[10000];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break;
else if (error)
throw boost::system::system_error(error);
cout << "Pergunta: " << data << endl;
string reply = askToServers(string(data)) + "\n";
cout << "Resposta: " << reply << endl;
boost::asio::write(*sock, boost::asio::buffer(
reply, reply.size()));
}
}catch (std::exception& e){
std::cerr << "Exception: clientListening: " << e.what() << "\n";
}
}
示例12: session
void session( socket_ptr sock ) {
try {
#ifdef VERBOSE
std::cout << "New client " << sock->remote_endpoint() << " connected." << std::endl << std::flush;
#endif
for (;;) {
boost::system::error_code error;
size_t length = sock->read_some( boost::asio::buffer( z, sizeof( Ccharacter ) ), error );
if ( error == boost::asio::error::eof ) {
#ifdef VERBOSE
std::cout << "Client " << sock->remote_endpoint() << " disconnected." << std::endl << std::flush;
#endif
break; // Connection closed cleanly by peer.
}
else if ( error )
throw boost::system::system_error( error ); // Some other error.
boost::asio::write( *sock, boost::asio::buffer( z, sizeof( Ccharacter ) ));
}
}
catch (std::exception& e) {
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
示例13: client_session
void network::client_session(socket_ptr sock){
using namespace boost::property_tree;
ip::tcp::endpoint remote_endpoint = sock->remote_endpoint();
cout<<"NEW CONNECTION BUILT:FROM:IP{"<<remote_endpoint.address().to_string().c_str()<<"},PORT:{"<<remote_endpoint.port()<<"}"<<endl;
connection_center.register_(remote_endpoint.address().to_string(),
remote_endpoint.port(),sock);
stringstream strings;
strings<<"WELCOME !! ["<<remote_endpoint.address().to_string().c_str()<<":"<<remote_endpoint.port()<<"] login.";
loader->process_request(sock,MSG_WELCOME,strlen(MSG_WELCOME));
try
{
while(true){
char data[MSG_LEN];
memset(data,0,MSG_LEN);
size_t len = sock->read_some(buffer(data));
if(len > 0){
cout<<"RECEIVE ["<<data<<"]FROM:IP{"<<remote_endpoint.address().to_string().c_str()<<"},PORT:{"<<remote_endpoint.port()<<"}"<<endl;
//同步 进行
loader->process_request(sock,data,strlen(data));
}
}
}
catch(boost::system::system_error & e)
{
cout<<"EXCEPTION TERMINATE: BY IP{"<<remote_endpoint.address().to_string().c_str()<<"},PORT:{"<<remote_endpoint.port()<<"}"<<endl;
connection_center.login_out(remote_endpoint.address().to_string(),
remote_endpoint.port());
loader->process_request(sock,MSG_DISCONNECT,strlen(MSG_DISCONNECT));
return;
}
cout<<"SESSION CLOSED OF IP{"<<remote_endpoint.address().to_string().c_str()<<"},PORT:{"<<remote_endpoint.port()<<"}"<<endl;
connection_center.login_out(remote_endpoint.address().to_string(),
remote_endpoint.port());
loader->process_request(sock,MSG_EXIT,strlen(MSG_EXIT));
return;
}