Home > other >  Chat about boost. An asio device is turned off, reopening, connections are not problems.
Chat about boost. An asio device is turned off, reopening, connections are not problems.

Time:01-27

Wrote a chat today, the service side hang to tencent can run on the cloud,
But found a little problems when running the client, the client for the first time run there is no problem, but is turned off, and then will open even the service side,
Special fan ~ ~ ~ ~
Then open the resource monitor, found the handle was associated with a client should have four explore. Exe, and PID is the same, feel the problem is out of here, but have no idea about this aspect, so want to ask everybody a great god hh
Exe client such as the following address
http://pan.baidu.com/s/1mh52wIW

The code below using static compilation
 # include & lt; Cstdlib> 
#include
#include
#include
#include
#include
#include


IP using boost: : an asio: : : : TCP;
using namespace std;
Typedef STD: : deque
The class chat_client
{
Public:
//link
Chat_client (boost: : an asio: : io_service & amp; The io_service,
TCP: : resolver: : iterator endpoint_iterator)
: io_service_ (io_service),
Socket_ (io_service)
{

TCP: : the endpoint endpoint=* endpoint_iterator;
Socket_. Async_connect (the endpoint,
Boost: : bind (& amp; Chat_client: : handle_connect, this,
Boost: : an asio: : placeholders: : error, + + endpoint_iterator));
}

Void the write (const chat_message & amp; MSG)//5
{

Io_service_. Post (boost: : bind (& amp; Chat_client: : do_write, this MSG));
}

Void the close ()
{

Io_service_. Post (boost: : bind (& amp; Chat_client: : do_close, this));
}

Private:

Void handle_connect (const boost: : system: : error_code & amp; The error,//2
TCP: : resolver: : iterator endpoint_iterator)
{

if (! The error)
{
//link is successful, binding,
Boost: : an asio: : async_read (socket_,
Boost: : an asio: : buffer (read_msg_. Data (), chat_message: : header_length),//read head,
Boost: : bind (& amp; Chat_client: : handle_read_header, this,//3
Boost: : an asio: : placeholders: : error));
}
Else if (endpoint_iterator!=TCP: : resolver: : iterator ())
{
//fail to link
Socket_. Close ();
TCP: : the endpoint endpoint=* endpoint_iterator;
Socket_. Async_connect (the endpoint,
Boost: : bind (& amp; Chat_client: : handle_connect, this,//2
Boost: : an asio: : placeholders: : error, + + endpoint_iterator));
}
}

Void handle_read_header (const boost: : system: : error_code & amp; The error)//3
{

if (! The error & amp; & Read_msg_. Decode_header ())
{

Boost: : an asio: : async_read (socket_,
Boost: : an asio: : buffer (read_msg_ body (), read_msg_. Body_length ()),//read the text,
Boost: : bind (& amp; Chat_client: : handle_read_body, this,//4
Boost: : an asio: : placeholders: : error));
}
The else
{
Do_close ();
}
}

Void handle_read_body (const boost: : system: : error_code & amp; The error)//4
{

if (! The error)
{

STD: : cout. Write (read_msg_. Body (), read_msg_. Body_length ());//output text content
STD: : cout & lt; <"\n";
Boost: : an asio: : async_read (socket_,
Boost: : an asio: : buffer (read_msg_. Data (), chat_message: : header_length),
Boost: : bind (& amp; Chat_client: : handle_read_header, this,
Boost: : an asio: : placeholders: : error));
}
The else
{
Do_close ();
}
}

Void do_write chat_message (MSG)//6
{

Bool write_in_progress=! Write_msgs_. Empty ();
Write_msgs_. Push_back (MSG);
if (! Write_in_progress)
{
Boost: : an asio: : async_write (socket_,
Boost: : an asio: : buffer (write_msgs_. Front (). The data (),
Write_msgs_. Front (.) length ()),//the speech content into buffer
Boost: : bind (& amp; Chat_client: : handle_write, this,
Boost: : an asio: : placeholders: : error));
}
}

Void handle_write (const boost: : system: : error_code & amp; The error)//7
{

if (! The error)
{
Write_msgs_. Pop_front ();
if (! Write_msgs_. Empty ())
{
Boost: : an asio: : async_write (socket_,
Boost: : an asio: : buffer (write_msgs_. Front (). The data (),//judge whether there is any extra content
Write_msgs_. Front (.) length ()),
Boost: : bind (& amp; Chat_client: : handle_write, this,
Boost: : an asio: : placeholders: : error));
}
}
The else
{
Do_close ();
}
}

Void do_close ()
{

Socket_. Close ();
}

Private:
Boost: : an asio: : io_service & amp; Io_service_;
TCP: : socket socket_;
Chat_message read_msg_;//memory read data from a buffer
Chat_message_queue write_msgs_;//write data buffer queue
};

Int main (int arg c, char * argv [])
{

Try
{
//input personal ID
STD: : cout & lt; <"Please input your name:";
Char name [chat_message: : max_body_length + 1]={0};
STD: : cin. Getline (name, chat_message: : max_body_length + 1);
Char login [11]="login OK!" , login_msg [513]={0};
Strcpy (login_msg, name);
Strncat (login_msg, login, strlen (login) + 1);
//connection
Boost: : an asio: : io_service io_service;
TCP: : resolver resolver (io_service);
TCP: : resolver: : query query (" 115.159.199.161 ", "23333");
TCP: : resolver: : iterator iterator=resolver. Resolve (query);
Chat_client c (io_service, iterator);
Boost: : thread t (boost: : bind (& amp; Boost: : an asio: : io_service: : run, & amp; The io_service)); nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related