问题
I have written a program for client and server. The program currently does the following:
- Server listens to an end point for a connection
- Client connects to the server
- server sends message on accepting a connection
- client receives the message
I'm doing this asynchronously. But, the problem is they can only send/receive one message. After that, they just terminate. Below is my code:
#include <iostream>
#include <vector>
#include<boost/asio.hpp>
std::vector<char> buff(256);
void SendHandler(boost::system::error_code ex){
std::cout << " do something here" << std::endl;
}
void ReadHandler(boost::system::error_code ex){
std::cout << " print the buffer data..." << std::endl;
std::cout << buff.data() << std::endl;
}
void Server(){
boost::asio::io_service service;
using namespace boost::asio::ip;
tcp::endpoint endpoint(tcp::v4(), 4000);
tcp::acceptor acceptor(service, endpoint);
tcp::socket socket(service);
std::cout << "[Server] Waiting for connection" << std::endl;
acceptor.accept(socket);
std::cout << "[Server] Accepted a connection from client" << std::endl;
std::string msg = "Message from server";
socket.async_send(boost::asio::buffer(msg), SendHandler);
service.run();
}
void Client(){
boost::asio::io_service service;
using namespace boost::asio::ip;
tcp::endpoint endpoint(address::from_string("127.0.0.1"), 4000);
tcp::socket socket(service);
std::cout << "[Client] Connecting to server..." << std::endl;
socket.connect(endpoint);
std::cout << "[Client] Connection successful" << std::endl;
socket.async_read_some(boost::asio::buffer(buff), ReadHandler);
service.run();
}
int main(int argc, char **argv) {
if(argc == 1){
std::cout << "Please specify s for server or c for client" << std::endl;
return -1;
}
if(argv[1][0] == 's'){
Server();
}
else{
Client();
}
return 0;
}
I want to scale this program so that:
- server can listen and client can send request indefinitely. More like a one way chat system.
- server being able to connect to multiple clients.
Putting the async_send()
and service.run()
in an infinite loop didn't help. It just prints the message over and over on the client side until the client terminates.
I'm fairly new to boost::asio
even to network programming
. Please tell me where and what I should modify in my code?
回答1:
Start with learning some of the basics. There are excellent tutorials and examples provided by the library that will walk you through concepts and examples of both synchronous and async servers.
http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio.html
Start at the beginning. Work through the basic concepts with timers and callbacks. Continue with the tutorial into the networking. Build on the core ideas over the course of a few hours by editing the tutorial programs to try out ideas on your own.
Then work your way over to the examples section. The 'Chat' example is really close to what you are looking to build. This specific example shows how to connections open by re-issuing async_reads in the read handlers. Asynchronous asio servers will service multiple clients as long as the service threads continue to run with the assigned async_accept task.
http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio/examples/cpp11_examples.html http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio/example/cpp11/chat/chat_server.cpp
来源:https://stackoverflow.com/questions/41474820/c-boost-asio-networking-server-client