In short, my question is: if you have a tcp server that will have only one active connection, you can simply create one data socket on the server side. In all the training programs, I see that a new socket is being created, and I do not understand why this should be so. Why not create one server socket and then open, close, reset it (which I originally hoped for async_accept somehow)?
More details:
I took an asio training course for an asynchronous day server and can make it compile and work. I can even change it for my application and make it work as I want :). However, my original approach did not work, and I do not understand why I really hope for your help.
Basically, I wanted to create a TCP server that will accept only one TCP client and ignore all the others if this first client is not disconnected. I did this with acceptor.close () and acceptor.open (), so when the first connection was accepted, I just closed the acceptor, and whenever I got an eof error, I opened the acceptor again to listen for new connections . I naively thought that since I only wanted to have one active connection, I only needed to create a single:
boost::asio::ip::tcp::socket socket_
Since I only have one data socket receiving data from the client, it seems too difficult to create an entire tcp_connection class, according to the tutorial, that, as far as I could tell, it returned a new socket built using io_service. (In the tutorial, I think that every time the server accepts a new connection, a new socket is created using this code):
class tcp_connection : public boost::enable_shared_from_this<tcp_connection> { public: typedef boost::shared_ptr<tcp_connection> pointer; static pointer create(boost::asio::io_service& io_service) { return pointer(new tcp_connection(io_service)); } tcp::socket& socket() { return socket_; } private: tcp_connection(boost::asio::io_service& io_service) : socket_(io_service) { } tcp::socket socket_; std::string message_; };
So I tried to use only one boost :: asio :: ip :: tcp :: socket. I initialized this in the constructor of my server class using io_service similarly to the manual above. My testing showed that my first client will connect and only after the first one disconnects, the second one connects. However, no matter what I did, the data buffer in the async_accept (socket _, .....) call will never be full. Initially, I just kept getting eof errors, then I tried to close the sock and reopen, which removed the eof error and gave a transport end error not related to the error. Obviously, I am doing something very stupid here, but I cannot understand what is wrong philosophically speaking with what I am trying to do. When I create a new socket using the training methodology, everything works as expected.
So my question is: can I just have one socket and do something like cancel, close, open? I wanted to bind or something else, but isn't that an async_accept problem?
I only use boost asio for a week, and this is my first forum post like this, so it's easy on me;).