2017-09-29 4 views
2

皆私は本当にあなたの助けが必要です。私はboost::asioを学んだし、私は日のために対処することができない2つの問題...ここ簡単な機能を持つboost :: asioサーバー

は自分自身で行う簡単なエコーサーバの例である:

int main(
{ 
    // crate a server, binding it and listening connections 

    // acceptor server; 

    //socket client 

    server.async_accept(client, boost::bind(accept_handle, _1, &server, &client)); 

    io_service.run(); 

    return 0; 
} 

void accept_handle(const boost::system::error_code& eCode, boost::asio::ip::tcp::acceptor* server, boost::asio::ip::tcp::socket* client) 
{ 
    char data[43]; 
    client->async_read_some(boost::asio::buffer(data, 20), boost::bind(read_handle, _1, _2, server, client)); 
} 

void read_handle(const boost::system::error_code& eCode, size_t bytes) 
{ 
    char data_buf[20] = "hello"; 
    client->async_write_some(boost::buufer(data, 5), boost::bind(write_handle, _1, _2, server, client)); 
} 

void write_accept(const boost::system::error_code& eCode, size_t bytes) 
{ 
    boost::asio::ip::tcp::socket newConnection(server->get_ioservice)); // taking he io_service of the server 

    server->async_accept(newConnection, boost::bind(accept_handle, _1, server, client)); 
} 

問題がサーバであるが1つのクライアントを受け入れ、それが他の保留中のクライアントを許可していません...どこ私が間違っているのここ

:いずれかが存在する場合、私はメモ帳で構文エラーのためにとても残念にこのコードを書きました。

事前にお手数をおかけしますようお願いいたします。それはaccept_handle機能でasync_acceptを呼び出すではないので

+0

async_acceptは1つの接続を受け入れます。 'accept_handle'関数で再度呼び出す必要があります。 –

+1

[複数のクライアントを同時に処理できるブーストサーバーを作成するにはどうすればよいですか?](https://stackoverflow.com/questions/31579362/how-do-i-create-a-boost-server-that- can-handle-multiple-clients-at-once) –

+0

リチャード・ホッジスは、サーバーがクライアントから読み込んでいるときに別の接続を受け入れるためにaccept_handleを呼び出す必要があるが、なぜwrite_handleからasync_handleを呼び出すのが間違っているのか? –

答えて

0

コードが一つだけの接続を受け入れることができます。

コードにはオブジェクトの存続期間にも問題があります。clientsを参照するには共有ポインタを使用することをおすすめします。Boost async_* functions and shared_ptr's

+0

@Gruffalo、 'write_accept'を' async_accept 'に変更していただきありがとうございます'。しかし、@ Aborのコードには 'write_accept'という関数があり、' newConnection'を作成してから 'async_accept'を呼び出します... – kenba

+0

oops、sorry :) ... –

関連する問題