Discussion:
[asio-users] Accept one connection only
Robert Bielik
2017-07-23 16:24:22 UTC
Permalink
I'm using the TCP server example (with acceptor. async_accept), but I'd like to modify it so that only a single connection should be possible. All other connection attempts should result in an error. What need I do ?

Regards
/Robert


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Allen
2017-07-23 16:36:18 UTC
Permalink
that's what should happen if you call async_accept once, then don't
call it again until (a) after a connection is made and closed; or (b)
the async_accept handler is called with an error
Post by Robert Bielik
I'm using the TCP server example (with acceptor. async_accept), but I'd like to modify it so that only a single connection should be possible. All other connection attempts should result in an error. What need I do ?
Regards
/Robert
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Robert Bielik
2017-07-23 16:46:50 UTC
Permalink
Tried that, i.e. only accepting the socket if zero connections are active, and upon the disconnect call async_accept), but still I have no problem using putty to connect to the server, with several instances. Granted, there is no "connection" with the server, but I'd like putty to just fail directly with an error.

So that doesn't work, it seems...

Regards
/R
-----Original Message-----
Sent: den 23 juli 2017 18:36
Subject: Re: [asio-users] Accept one connection only
that's what should happen if you call async_accept once, then don't call it
again until (a) after a connection is made and closed; or (b) the async_accept
handler is called with an error
Post by Robert Bielik
I'm using the TCP server example (with acceptor. async_accept), but I'd like
to modify it so that only a single connection should be possible. All other
connection attempts should result in an error. What need I do ?
Post by Robert Bielik
Regards
/Robert
----------------------------------------------------------------------
-------- Check out the vibrant tech community on one of the world's
most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging
tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Robert Bielik
2017-07-23 17:06:40 UTC
Permalink
If, when accepting the first client, I call acceptor_.close() I get the behaviour I want, but after that I can't get the acceptor active again, i.e. even a single client connection fails...

A bit like this example (https://stackoverflow.com/a/5260390/255635), though haven't gotten it to start accepting connections again...

Regards
/R
-----Original Message-----
Sent: den 23 juli 2017 18:47
Subject: Re: [asio-users] Accept one connection only
Tried that, i.e. only accepting the socket if zero connections are active, and
upon the disconnect call async_accept), but still I have no problem using
putty to connect to the server, with several instances. Granted, there is no
"connection" with the server, but I'd like putty to just fail directly with an
error.
So that doesn't work, it seems...
Regards
/R
-----Original Message-----
Sent: den 23 juli 2017 18:36
Subject: Re: [asio-users] Accept one connection only
that's what should happen if you call async_accept once, then don't
call it again until (a) after a connection is made and closed; or (b)
the async_accept handler is called with an error
On Sun, Jul 23, 2017 at 12:24 PM, Robert Bielik
Post by Robert Bielik
I'm using the TCP server example (with acceptor. async_accept), but I'd like
to modify it so that only a single connection should be possible. All
other connection attempts should result in an error. What need I do ?
Post by Robert Bielik
Regards
/Robert
--------------------------------------------------------------------
--
-------- Check out the vibrant tech community on one of the world's
most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
----------------------------------------------------------------------
-------- Check out the vibrant tech community on one of the world's
most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging
tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Allen
2017-07-23 17:32:55 UTC
Permalink
FYI, calling acceptor_.close() will throw an exception on error. In
comparison, calling:

boost::system::error_code ec;
m_acceptor.close(ec);

will return the error in ec instead of throwing an exception.
Post by Robert Bielik
If, when accepting the first client, I call acceptor_.close() I get the behaviour I want, but after that I can't get the acceptor active again, i.e. even a single client connection fails...
A bit like this example (https://stackoverflow.com/a/5260390/255635), though haven't gotten it to start accepting connections again...
Regards
/R
-----Original Message-----
Sent: den 23 juli 2017 18:47
Subject: Re: [asio-users] Accept one connection only
Tried that, i.e. only accepting the socket if zero connections are active, and
upon the disconnect call async_accept), but still I have no problem using
putty to connect to the server, with several instances. Granted, there is no
"connection" with the server, but I'd like putty to just fail directly with an
error.
So that doesn't work, it seems...
Regards
/R
-----Original Message-----
Sent: den 23 juli 2017 18:36
Subject: Re: [asio-users] Accept one connection only
that's what should happen if you call async_accept once, then don't
call it again until (a) after a connection is made and closed; or (b)
the async_accept handler is called with an error
On Sun, Jul 23, 2017 at 12:24 PM, Robert Bielik
Post by Robert Bielik
I'm using the TCP server example (with acceptor. async_accept), but I'd like
to modify it so that only a single connection should be possible. All
other connection attempts should result in an error. What need I do ?
Post by Robert Bielik
Regards
/Robert
--------------------------------------------------------------------
--
-------- Check out the vibrant tech community on one of the world's
most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
----------------------------------------------------------------------
-------- Check out the vibrant tech community on one of the world's
most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging
tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio

Allen
2017-07-23 17:21:01 UTC
Permalink
In that case, you probably have to close the acceptor as soon as one
connection is made, and then open it again when the connection is
closed. In other words, there is probably code in there that looks
something like:

boost::asio::ip::tcp::acceptor m_acceptor(m_io_service);
m_acceptor.open(endpoint.protocol());
m_acceptor.bind(endpoint);
m_acceptor.listen(backlog);
m_acceptor.async_accept(m_socket, boost::bind(&Server::HandleAccept,
this, boost::asio::placeholders::error));

In your HandleAccept, you need to call:

boost::system::error_code ec;
m_acceptor.close(ec);

And then restart the whole thing (open, bind, listen, async_accept)
after the connection is closed.
Post by Robert Bielik
Tried that, i.e. only accepting the socket if zero connections are active, and upon the disconnect call async_accept), but still I have no problem using putty to connect to the server, with several instances. Granted, there is no "connection" with the server, but I'd like putty to just fail directly with an error.
So that doesn't work, it seems...
Regards
/R
-----Original Message-----
Sent: den 23 juli 2017 18:36
Subject: Re: [asio-users] Accept one connection only
that's what should happen if you call async_accept once, then don't call it
again until (a) after a connection is made and closed; or (b) the async_accept
handler is called with an error
Post by Robert Bielik
I'm using the TCP server example (with acceptor. async_accept), but I'd like
to modify it so that only a single connection should be possible. All other
connection attempts should result in an error. What need I do ?
Post by Robert Bielik
Regards
/Robert
----------------------------------------------------------------------
-------- Check out the vibrant tech community on one of the world's
most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging
tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Continue reading on narkive:
Loading...